Question regarding reading and writing from/to file

#include etc.
using std::etc

int main (int argc, char** argv)
{
ifstream inputFileStream;
ofstream outputFileStream;
string one, two, three, four, five, six;


inputFileStream.open("Rank.txt");

if(inputFileStream.fail())
{
cerr << "File Not Found";
exit(1);
};

outputFileStream.open("Beta.txt");
if (outputFileStream.fail())
{
cerr << "permission error: cannot create file";
return 1;
};

while(inputFileStream >> one >> two >> three >> four >> five >> six)
//ASSUMING THERE ARE LESS THEN 6 STRINGS PER LINE
{
outputFileStream << four << " " << one << endl;
};

inputFileStream.close();
outputFileStream.close();

return 0;
};

Sample Rank.txt file:
1 fruit ranked Apples comment junk
2 fruit ranked Oranges
3 fruit ranked Blue Berries comment junk
4 fruit unranked Bananas

Hello everyone. I have a question regarding reading lines from file.
The main question is, Is there a way to stop reading a line mid way(or whenever you choose) and go to the next line?
So in my above code, is there a way that i could stop reading at four and go to the next line and keep doing so until eof? I don't know if there is a type of endline or endofline marker that i can put.
Last edited on
closed account (1vD3vCM9)
To read from files you need to #include <fstream>
And after that, you do this for an example:
std::ifstream readData;
std::ofstream writeData;
readData.open("file.txt");
If(readData.fail())
{
perror("file.txt");
}
std::string lines [100];
for(int j = 0; 0 < 100; i++)
{
std::getline(readData, lines[i],'\n');
}
closed account (1vD3vCM9)
That should get you to understand the basics.
If it's not enough, search in Google: C++ input and output with files.
There is a guide in this site about it.
Good luck!
Topic archived. No new replies allowed.