-
my problem here is how do i read from the infile that one line and write it then when its done go onto the next one. I am unfamiliar with fstream any help would be greatful. I know how to open and close the files, thats about all. well also that to read from an infile i use >> rather than << i think.
-
Can you read from cin? It's very similar. getline reads a line, operator>> reads one word.
-
no i have to read from the infile
-
this is the code i've come up with so far. I figure i can make word equal to the line of the string its reading and loop through going word[i] printing all the chars to the outfile, then put an if statement saying if word[i] = '@' then reverse. Then when it gets to the end of the line go to the next and start all over.
Code:
#include <stack>
#include <iostream>
#include <fstream>
using namespace std;
stack<char> rev;
char tmp;
int counter;
void Reverse()
{
while(word[i] != '@')
{
rev.push(word[i]);
++counter;
}
for(i=1;i<=counter;i++)
{
tmp = rev.top();
outFile << tmp;
rev.pop();
}
}
int main()
{
string inFileName = "reverse.in";
string outFileName = "reverse.out";
ifstream inFile;
ofstream outFile;
-
>> no i have to read from the infile
I said it is similar. I didn't mean that you should use cin. If you know how to use cin, then you can do (almost) the exact same thing and use infile instead.
You've still got a ways to go with your code, but the idea seems good. I would do less code before compiling and testing. For example, start by just reading in each word and outputting it without paying any attention to the @ symbol and without using the stack. This will give you practice with using an ifstream. Once you get the code working so that it copies the words exactly, then you can add more code to try to handle the reversing part.