File Handling

a question on file handling in C++

Page 1 of 1

6 Replies - 1761 Views - Last Post: 21 July 2009 - 01:38 AM Rate Topic: -----

#1 jral27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-July 09

File Handling

Posted 18 July 2009 - 09:59 PM

hello, i'm a student and i've just recently started on learning c++. right now, i'm having trouble on how to properly read a character one by one until the end of file.

here's that part of the code:

ifstream fin;
ofstream fout;

int readFile(ifstream& fin)
{
	char next = fin.peek();
	int line_count=1;
	
	if (next == fin.eof())
	{
		  cout << "end of file" << endl;
		  return 0;
		  } else{
					   
		  do
		  {
		  fin.get(next);
		  cout << next;
		  if(next=='\n') line_count++;
		  }	 while(next=='\n' || next=='\t' || next==' ');
		  
		  if (fin.eof())
		  {
			 cout << "end of file" << endl;
			 return 0;
			 } 
	
			 else
			 {
			   if(isalpha(next)){
			 	do{
 				   fin.get(next);
				   cout << next;
				   }while(isalnum(next) || next=='_');
			   } 
		
			   else if(isdigit(next))
			   {
				do{
				   fin.get(next);
				   cout << next;
				   }while(isdigit(next));
			   }
		
			   else if(next=='&')
			   {
				fin.get(next);
				  cout << next;
		   		if(next=='&') fin.get(next);
				}
			
			   else if(next=='|')
			   {
				  fin.get(next);
				cout << next;
				if(next=='|') fin.get(next);
				}
			
			   else if(next=='>' || next=='<' || next=='=')
			   {
				fin.get(next);
				cout << next;
				if(next=='=') fin.get(next);
							  }
			 
				else{
					 fin.get(next)
					  }
				  } 
				  
				  if(!fin.eof()) fin.putback(next);
				  return 1;
		 }
	}
}



my main is just basically opening the file and calling the function.
when i ran the program, the last character gets printed repeatedly. does anybody have an idea where i went wrong? or some suggestions would be appreciated :)

p.s. i'm making a lexical analyzer and that part of the code is supposed to be where i read the file, and yes, i haven't started with the other functions yet. i wanted to finish this function first before moving on to a new one.

This post has been edited by jral27: 18 July 2009 - 10:07 PM


Is This A Good Question/Topic? 0
  • +

Replies To: File Handling

#2 FoodGood  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 97
  • Joined: 08-July 09

Re: File Handling

Posted 18 July 2009 - 11:01 PM

When you read a file, the best way i know is to do a "while(EOF)" loop
you don't have to ask several times if you've reached to EOF..
you can do something like this:

char next; 
fin.get(next);
while (!fin.eof())
{
	//Do your thing
}
fin.close();

Was This Post Helpful? 0
  • +
  • -

#3 jral27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-July 09

Re: File Handling

Posted 18 July 2009 - 11:54 PM

i did think of doing it like that, but i was just wondering, if i read everything, as in the whole file from beginning to end, will i still be able to check the grammar or something?

i made the code the way it is in order to read one special character, letter, or digit at a time, then check if there's an existing token for the said characters. it's my first time making a lexical analyzer so i really have no clue if i'm starting off alright.
Was This Post Helpful? 0
  • +
  • -

#4 codefreak.  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 79
  • Joined: 11-July 09

Re: File Handling

Posted 19 July 2009 - 07:04 AM

http://www.dreaminco...topic114254.htm
Was This Post Helpful? 0
  • +
  • -

#5 FoodGood  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 97
  • Joined: 08-July 09

Re: File Handling

Posted 19 July 2009 - 11:46 AM

View Postjral27, on 18 Jul, 2009 - 10:54 PM, said:

i did think of doing it like that, but i was just wondering, if i read everything, as in the whole file from beginning to end, will i still be able to check the grammar or something?

i made the code the way it is in order to read one special character, letter, or digit at a time, then check if there's an existing token for the said characters. it's my first time making a lexical analyzer so i really have no clue if i'm starting off alright.


I think you can do anything you want in this loop
I didn't read all of your code, but from what I've seen, there will be no problem
You can use the "switch" block to ask about special characters, and after the "switch" you should always read a character from the file..
I think it will make tour code cleaner, and easier for you
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: File Handling

Posted 19 July 2009 - 04:47 PM

View PostFoodGood, on 18 Jul, 2009 - 10:01 PM, said:

When you read a file, the best way i know is to do a "while(EOF)" loop
you don't have to ask several times if you've reached to EOF..
you can do something like this:

char next; 
fin.get(next);
while (!fin.eof())
{
	//Do your thing
}
fin.close();


why are you reading by char? how big are the files?
this method can not handle larger files and will also take forever.
You should read line by line.

and also EOF method is not the best way.


ifstream input("File.txt");
string line = "";
int count = 0;
while(getline(input,line))
{
count++;
cout<<line<<endl;
}


Was This Post Helpful? 0
  • +
  • -

#7 jral27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-July 09

Re: File Handling

Posted 21 July 2009 - 01:38 AM

View PostImaSexy, on 19 Jul, 2009 - 03:47 PM, said:

why are you reading by char? how big are the files?
this method can not handle larger files and will also take forever.
You should read line by line.

and also EOF method is not the best way.


ifstream input("File.txt");
string line = "";
int count = 0;
while(getline(input,line))
{
count++;
cout<<line<<endl;
}




i'm reading by char because it's the only way i know in order to read by word. and i need to read by word coz i need to find out whether that specific word i just read is defined or has a token. so yes, im probably going to call the function a lot in my code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1