Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 11th, 2006, 5:55 PM   #1
KTiger
Newbie
 
KTiger's Avatar
 
Join Date: May 2006
Location: Narvik, Norway
Posts: 22
Rep Power: 0 KTiger is on a distinguished road
C++ examples

Hi!
I have used VB6 for years and resently moved to Gambas (linux-version of VB), and I have some trouble too learn C++, why isn't there any simple examples on this in linux,, ie. "man fopen" does not give me any examples :mad: (I really liked the old QBasic help system, is there some for C++?)

In VB6 I could write a file with doing follow:

  string myfile;
  string temp;

  open myfile on #1 for input     // something like that... its a while since I have used VB.
  while not eof(1)
  (
    input line #1, temp
    print temp;
  )
  close #1

how is this done in C++ (G++ actually), to read line by line, and print it to the screen.
I have managed to do this:
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
  char string1[20], string2[20], string3[30];
  ifstream fin("file.dat");

  if (!fin.is_open()) {
    cerr << "Error: Could not open file." << endl;
    exit(1);
  }

  fin >> string1;
  fin >> string2;
  fin >> string3;

  cout << string1 << " " << string2 << " " << string3 << endl;

  fin.close();

  return 0;
}
but this does not give me a loop to cycle through the file, plus it seems to remove some spesial chars like "\n" why?
And still I got alot of other questions, but those I'll take later.

plz, forgive me if there some english errors..
KTiger is offline   Reply With Quote
Old May 11th, 2006, 6:19 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Google. You'll be inundated. You can also search THIS forum for links. You may not find anything specific to fopen, but you'll find plenty of links to good tutorials. Loops are control constructs; one places them wherever one needs repetetive operations. They aren't restricted to file operations, but come in handy there, as well. There are many input and output functions over and above the insertion and extraction operators (<< and >>). Why? Because they don't serve all purposes well. Pick and choose, just as you do on the candy aisle (or, at least, I do). Read the documentation for what you pick. Find how it informs you of failure and be sure to ask it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 11th, 2006, 7:41 PM   #3
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3 Jimbo is on a distinguished road
Quote:
Originally Posted by KTiger
how is this done in C++ (G++ actually), to read line by line, and print it to the screen.
C++ is the language, g++ is a compiler.
Jimbo is offline   Reply With Quote
Old May 11th, 2006, 10:47 PM   #4
Prm753
Professional Programmer
 
Prm753's Avatar
 
Join Date: Oct 2005
Location: United States
Posts: 447
Rep Power: 4 Prm753 is on a distinguished road
Send a message via AIM to Prm753 Send a message via MSN to Prm753
Did you take a look at this:

http://www.cs.odu.edu/~jbollen/CS149.../cpp_files.pdf

This is quite informative about fin and fout.
__________________
The world's first athletic computer geek!
The home of PrProgramsStudios
How not to post a question: <-- Please don't reply
Prm753 is offline   Reply With Quote
Old May 12th, 2006, 5:27 AM   #5
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
fopen is C. Using the ifstream constructor is C++.
Do you want something like:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
	string string1, string2, string3;
	ifstream fin("file.dat");

	if (!fin.is_open())
	{
		cerr << "Error: Could not open file." << endl;
		exit(1);
	}

	getline(fin, string1);
	getline(fin, string2);
	getline(fin, string3);

	cout << string1 << endl << string2 << endl << string3 << endl;

	fin.close();

	return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old May 12th, 2006, 7:17 AM   #6
KTiger
Newbie
 
KTiger's Avatar
 
Join Date: May 2006
Location: Narvik, Norway
Posts: 22
Rep Power: 0 KTiger is on a distinguished road
Wow, so many responses in soo little time... (I'll stick to this forum from now on)

Thanks nnxion, getline is what I have been searching for along time.
Now I need to figure out how to read through any file, since I dont know how to detect the EOF.
( The final idea is to read the entire file in to a string[] , so I can make a very simple editor)

Jimbo:
C++ is the language, g++ is a compiler.
Jimbo your right, I dont know why I'd mixed them. :o
KTiger is offline   Reply With Quote
Old May 12th, 2006, 7:20 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
READ your documentation. If you don't know how to detect EOF, it's clear you haven't.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 12th, 2006, 7:50 AM   #8
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by DaWei
READ your documentation. If you don't know how to detect EOF, it's clear you haven't.
As DaWei says, it's important to read your documentation. If you don't have money to buy a book it's probably a good idea to read a tutorial, like here.

Try to do the best you can and try examples out on your compiler, learn to use the debugger, he's among my best friends when it comes to programming. (That's just an expression, if you're worried )

If you're still stuck at that point, then come to us and ask us for help.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:56 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC