By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,304 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

file handling

P: 5
i want to know abt some commands iwant to know how to convert strings in an input file to some other string in output file.
example:
input:10 20 40 30
11 37 49 60
output:ADD 20 40 30
sub 37 49 60
Mar 9 '07 #1
Share this Question
Share on Google+
8 Replies


Ganon11
Expert 2.5K+
P: 3,652
So 10 will be exchanged with ADD, and 11 will be exchanged with sub, but the rest of the numbers stay the same...

Why not repeat this process for every line in the file:

1) Read the first number
2) If its 10, output ADD, if 11 output sub, if x do y...etc
3) Output the rest of the line
4) Start over
Mar 9 '07 #2

P: 5
So 10 will be exchanged with ADD, and 11 will be exchanged with sub, but the rest of the numbers stay the same...

Why not repeat this process for every line in the file:

1) Read the first number
2) If its 10, output ADD, if 11 output sub, if x do y...etc
3) Output the rest of the line
4) Start over
ya i know this algo but i m very much new to this so i don't know the commands pls give me commands if u can
Mar 12 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
You will have to look up the ifstream and ofstream classes in the header file fstream. You can use these to read the file into your variables and then output it to a new file.
Mar 12 '07 #4

P: 5
You will have to look up the ifstream and ofstream classes in the header file fstream. You can use these to read the file into your variables and then output it to a new file.
ok here i hve tried smthng but it's not working:
Expand|Select|Wrap|Line Numbers
  1. #include<fstream.h>
  2. #include<iostream.h>
  3. #define Mfile "temp.dat"
  4. fstream temp;
  5. int main()
  6. {
  7. char ifile[40],ofile[40];
  8. char str[40];
  9. fstream fin;
  10. fstream fout;
  11. cout<<"enter the input file:\n";
  12. cin>>ifile;
  13. cout<<"enter the output file:\n";
  14. cin>>ofile;
  15. fin.open(ifile,ios::in);
  16. fout.open(ofile,ios::out);
  17. int num,n1,n2,n3;
  18. while(fin.getline (str,40))
  19. {
  20.  temp.open("Mfile",ios::out);
  21.  temp<<str;
  22.  temp.close();
  23.  temp.open("Mfile",ios::in);
  24.   while(fin>>str)
  25. {
  26. if(num==10 && n1==20 && n2==30 && n3==40)
  27. cout<<"add"<<" "<<"20"<<"30"<<"40";
  28. }
  29. }
  30. fin.close();
  31. fout.close();
  32. }
this prog is asking for input file and also generating output file but nothing is printed in it..so pls debug it if u can
Mar 12 '07 #5

Ganon11
Expert 2.5K+
P: 3,652
Instead of using two fstream objects and supplying them ios::in and ios::out, I would use an ifstream object and an ofstream object, like this:

Expand|Select|Wrap|Line Numbers
  1. ifstream IN;
  2. ofstream OUT;
  3.  
  4. // get input file name
  5. // get output file name
  6.  
  7. IN.open(inFile);
  8. OUT.open(outFile);
Now you can use IN just like you would use cin, and OUT just like you would use cout.
Mar 12 '07 #6

P: 5
It's just due to the inclusion of smiley icons in the forum. I know it's supposed to be : o, so it didn't confuse me too much :).
ok
then in while loop i will read the input file ??
Mar 12 '07 #7

Ganon11
Expert 2.5K+
P: 3,652
Your while loop will continue reading the file while there is still data to process, correct?

Then you should use IN.eof() to control your loop. .eof() returns true if it has reached the end of the input file, false otherwise. This way, you can continue getting data and performing operations on it until you run out of data.
Mar 12 '07 #8

P: 5
ok
then in while loop i will read the input file ??
pls do whatever u like to do and tel me exactly what i hve to do in this prog.
in this thr is no syntax error
Mar 12 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.