Tuesday, July 14, 2009

Data handling( very basic program)?

i want to write a program to display the contents of file using get() function. i did not get output in Turbo C++. why???


please help me





# include%26lt;fstream.h%26gt;





int main()


{


char ch;


ifstream fin;


fin.oprn("marks.dat",ios::in);


if(!fin)


{


cout%26lt;%26lt;"\n can't open file";


return 1; // what does this statement do????


}


while(fin)


{


fin.get(ch);


cout%26lt;%26lt;ch;


}


fin.close();


return 0;


}

Data handling( very basic program)?
Not sure if this is just a typo, but you have shown here:





fin.oprn("marks.dat",ios::in);





that should be open, with an 'e'





But since you're not getting a compile error, I'm guessing that's not the problem.





while(fin) will always return true, therefore it will never exit the loop.





try this while loop instead:





while(fin.get(ch))


{


cout %26lt;%26lt; ch;


}





And you ask in your code what return 1; does..





main() is a function, and since you put "int main()" you're saying this function will return an integer. When you put return 1, you're saying "I've found the answer, it is 1, so we can exit the function now with the answer of 1"





In other words, it exits the function at that point. If you had some other function calling main() from somewhere else, you could use that data. But since you aren't, it doesn't really matter what number you return.





Make sense?

flower garden

No comments:

Post a Comment