-
appending to a file
This should be easy to answer.
I have a file with many lines of numbers
ie: 3+4/8
8-(5+3)
....
Anyway, I need to add the $ character to the end of each line. I forgot how to do this however. I was trying to do a search for a new line and then insert, but I forgot if when doing a while loop to check if it tries to add the character afterwards...Take a look at my code and give any suggestions.
Thanks
Code:
#include <iostream>
#include <fstream>
using namespace std;
ifstream infile;
ofstream outfile;
int main()
{
char tok;
infile.open("data.dat");
infile >> tok;
while (!infile.eof())
{
while(tok='\n')
{
outfile << '$';
infile >> tok;
}
}
return 0;
}
-
should be Also, you never opened an output file, and even if you had, you aren't ever actually outputting the token, you are only outputting the '$'. Your output file would just be:
$$$$$$$$$$$$$$$$$$$$$$$$
Try fixing those problems first and then see if you can figure out the rest.
-
Okay, I have decided to create a new file to copy the data to along with the end tolkien. It copies the data without the endlines which makes me believe I might need to use getline(). It still doesn't add the end tolkiens obivously. I'll look through some old notes to see.
Here is my new code:
Code:
#include <iostream>
#include <fstream>
using namespace std;
ifstream infile;
ofstream outfile;
int main()
{
char tok;
infile.open("infix.dat");
outfile.open("infix2.dat");
infile>>tok;
while(!infile.eof())
{
outfile<<tok;
if(tok=='\n')
{
outfile << '$';
}
infile>>tok;
}
-
You're right. You will have to use getline. I totally forgot that >> skips whitespace, even going one char at a time.
Of course, getline is probably easier, since you don't have to check for the newline anymore. Your code looks fine to me except for that one problem.
-
okay, hopefully this will be my last post .. I got it to work the way I wanted but one last thing. I need to edit the file I am writing to right after I write to it.
I tried this new code jsut to test to see if it works, but it says it won't read from the file.
Here is the code:
Code:
#include <iostream>
#include <fstream>
using namespace std;
ifstream infile;
ofstream outfile;
int main()
{
char tok;
infile.open("infix.dat");
outfile.open("infix2.dat");
infile.get(tok);
while(!infile.eof())
{
outfile<<tok;
if(tok=='\n')
{
outfile << '$';
}
infile.get(tok);
}
infile.open("infix2.dat");
if(infile.fail())
{cout << "error";
return false;}
else
cout << "Please work"<<endl;
return 0;
}
-
Don't forget to close the output file when you are done writing to it, and the input file while you're at it.
-
tried that.. still gives me "error"
Code:
#include <iostream>
#include <fstream>
using namespace std;
ifstream infile;
ofstream outfile;
int main()
{
char tok;
infile.open("infix.dat");
outfile.open("infix2.dat");
infile.get(tok);
while(!infile.eof())
{
outfile<<tok;
if(tok=='\n')
{
outfile << '$';
}
infile.get(tok);
}
infile.close();
outfile.close();
infile.open("infix2.dat");
if(infile.fail())
{cout << "error";
return false;}
else
cout << "bite me"<<endl;
return 0;
}
-
Anyone have any idea on this last problem. It is the last thing I am stuck on.
Thanks
-
Try this one
Code:
infile.close();
outfile.close();
infile.clear(); // reset the stream
infile.open("infix2.dat");
-
awesome, thanks... i forgot about that fxn