Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Stream HELP

Status
Not open for further replies.

topdawgg19

Programmer
Sep 26, 2004
9
US
Alright I doubt anyone will be able to figure this one out... My program gets stuck right after I read in all the information from the file. The output screen never shows anything past
while (! program_in.eof() )
{
program_in.getline (source,100);
cout << source << endl;
}
Anotherwords, if I put in a cout<<"I HATE THIS PROGRAM"<<endl right after the previous set of lines above... it will not print this to my screen. But the above lines do give me the contents of source. Now for the fun part, if I run the debugger to this line: while (source[start]!=EOF); it shows that source[start]= a which is correct. Any ideas??

==========================================================
int main()
{
char file_in[250]="c:/Documents and Settings/All Users/Desktop/input.txt";
//cout<<"Enter the Input File "<<endl;
//cin>>file_in;

ifstream program_in(file_in,ios::in); //sets file for input

if(!program_in) //checks to see if file exists
{
cerr<<"File Not Found"<<endl;
exit(1);
}
int i=0;

while (! program_in.eof() )
{
program_in.getline (source,100);
cout << source << endl;
}

program_in.close();
cout<<endl<<"hello";
while (source[start]!=EOF);
{
lexer();
cout<<token<<" "<<lexeme<<endl;
}

return 0;
}
 
1. Always use this forum markup language (TGML) tag CODE to select sources (see Process TGML link on the form).
2. Use backslashes in Windows filenames:
Code:
const char file_in[]="c:\\Documents and Settings"
                     "\\All Users\\Desktop\\input.txt";
It's not a best place for debugging stage files...
3. What is it your source variable? Array? Where is its declaration? If we declare char source[101] (or larger), the program reads all input OK (only if its line length limit less than 100).
4. It's a very strange condition in the last loop:
Code:
source[start] != EOF
You never gets EOF in this string with stream.getline()!
More seriously (and curiously;):
Code:
while (source[start]!=EOF); // Semicolon!!! while terminated
{
   lexer();
   cout<<token<<" "<<lexeme<<endl;
}
See semicolon after while(). It's your loop body: null statement. Now you have loop forever - that's all...
5. You snippet do nothing (useful) now - but it's the other story. Correct it then go on?...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top