I am having trouble understand how to read a file and then print it out. Below the code is suppose to write name and grade to a text file and then read the text file and display. I can write to the text file, but i'm not understand how to read from it cause it is not working at all.[b].Please ignore the code that is commented out..[/b] the text file is suppose to be called grade...enter a 0 to input data..enter -1 to stop input.
What do you mean by "it is not working at all"? Does it make the bears in your area angry at you? Does it install Internet Explorer with a virus? Does it cause your neighbor's house to implode? You need to be specific.
ok so i change the last part of the code to this to read txt file.. the program will run.. but once it gets to the read portion..it stops and exit program and says process exited with return value 0.
Put an opening curly brace { on line 24 and a closing curly brace } on line 59. Otherwise the file is still open for output when you try to open it for input.
ok I made those changes.. again the program will write to txt file but i cant get it to read it.. here is my new adjusted code.. still doesnt work right
this is what I get when i run the program... notice the read portion of the program is not showing..
Enter file name: grade
Enter a name (-1 to end or 0 to continue):0
Enter name: test1
Enter grade : 88
Enter a name (-1 to end or 0 to continue):-1
can not open a file
--------------------------------
Process exited with return value 0
Press any key to continue . . .
all the example i see have ifstream..stream name..and then
streamname.open.. even the powerpoints from my class..
i change the code to this..and its working better. it seems to be reading the file now.. only problem is the output looks like its showing the address not the value...
Ok..right now your just confusing me.. Can you write a simple code that shows you writing to a file that doesn't already exist and then reading from that text file. and please make sure there is both char and numbers written to and read from the text file.
I'm not asking you to do my work above..I just need to see an example..because right now I am totally confused and your question are not helping me at all..
#include <iostream>
#include <string>
#include <fstream>
int main()
{
{
std::ofstream out ("data.txt"); //file is created if it doesn't exist
for(unsigned i = 0; i < 10; ++i)
{
std::cout << "Enter description for #" << i << ": " << std::flush;
std::string desc;
std::getline(std::cin, desc);
if(!desc.empty()) //allows you to skip some numbers, just an example
{
out << i << " " << desc << std::endl;
}
}
}
std::cout << std::endl << std::endl;
{
std::ifstream in ("data.txt");
unsigned id;
std::string desc;
while(std::getline((in >> id).ignore(1), desc)) //this may look confusing
{
std::cout << "#" << id << " = \"" << desc << "\"" << std::endl;
}
}
}
The while loop in the second half of the code may seem confusing, but all it does is read into id, then ignore the space after it, and then read in the description to the next newline. The reason for it being in the loop condition is so that if any input operation fails (such as trying to read at the end of the file), the loop will end.
Ok that was kind of above my head now..I am a beginner..this is example of code we used in class to read text.. can you help me using this technique to read the data from text in my program..here is the code they gave us as example.
Ok so I applied your example..here is my code changes ..However now it runs but again it does not seem to do the read portion, or at least does not display result..