Connect with Expertise | Find Experts, Get Answers, Share Insights

Read/Write file in c++

 
Join Date: May 2007
Posts: 11
#1: May 29 '07
Dear friends,

im a newbee for this forum and c++ im doing my MSc in Simulation Tech in mech. Engineering. My knowledge of c++ is very little which I had during my UG studies Long long ago .I am now forced to do some programming as a small part of my thesis work. Here goes my task and question.

I want to read the text file and jus find the displacement old value and replace them with new value and write them in another file.........(In my analysis, an input file has been created contains some variables.For ex: a displacement value the the only variable for several analysis which i need to change to perform different analysis)

Jus I made a small program for it by using all my C++ knowledge (!!!!!!!)

To replace 0.000(old value) to 1.413(new value) and write it in another file .

I didn’t use getline () function as it is trying to replace the whole line at once even if there r two values to replace within a line.

I donno whether it is possible using getline function(I didn’t succeed replacing all the values in a line by using it)


Here comes my text file

Samp1.txt: contains

0.000 God save us programmers

I don’t do programming here after 0.000 for sure 0.000



Expand|Select|Wrap|Line Numbers
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     string a;
  9.     string string1;
  10.     string string2 = "1.413";
  11.     string string3 = "0.000";
  12.  
  13.     fstream inputFile("C:\\TEMP\\samp1.txt");
  14.     fstream outputFile;
  15.     outputFile.open("C:\\TEMP\\samp2.txt");
  16.  
  17.     while (!inputFile.eof()) {
  18.         inputFile >> a;
  19. //cout << a ;
  20.         int spot = a.find(string2);
  21.  
  22.         if (spot >= 0) {
  23.             cout << "found at" << spot << " the string" << string2 << endl;
  24.             string1 = a;
  25.             int b = string3.length();
  26.             string1.replace(spot, b, string3);
  27.             a = string1;
  28.             cout << a << endl;
  29.             cin.get();
  30.         }
  31.         outputFile << a << endl;
  32.  
  33.     }
  34.     outputFile.close();
  35. }
  36.  
Output:
Samp2.txt: written file contains output like

1.413
God
save
us
programmers
I
don’t
do
programming
here
after
1.413
for
sure
1.413
1.413 (****)


1. I want to hav my output file looks the same as my input file (only with a change of value replacement) not like this now I hav
2. while loop executes eof () true value at the end of file and prints again the value “a” (which stored last …. refer output (****))


may b sound simple and sillyfor u, but for me its complex

i want my output looks like

Samp2.txt:

1.413 God save us programmers

I don’t do programming here after 1.413 for sure 1.413


Will you give me some sugesstion to get my output in such a manner?

some new functions or methods?

Savage's Avatar
E
C
 
Join Date: Feb 2007
Posts: 1,739
#2: May 29 '07

re: Read/Write file in c++


Problem is probably here:

inputFile >> a;

this extractor will input data in string a until reaching a whitespace.

Try using getline()

Savage
 
Join Date: May 2007
Posts: 11
#3: May 30 '07

re: Read/Write file in c++


Problem is probably here:

inputFile >> a;

this extractor will input data in string a until reaching a whitespace.

Try using getline()

Savage

Thx savage

as i mentioned earlier, by using getline() function i hav not succeed

can u tell me how can i replace a particular string from a line

int main()
{
string a;
string string1;
string string2="0.000";
string string3="1.413";
string line;
fstream inputFile("C:\\TEMP\\samp1.txt");
fstream outputFile;
outputFile.open("C:\\TEMP\\samp2.txt");
while(!inputFile.eof())
{
//inputFile >> a;
getline(inputFile, line);
cout << line <<endl;

cout <<"line is "<< line << endl;
cin.get();
int spot = line.find(string2);

if(spot >= 0)
{
cout<<"found at"<<spot<<" the string" <<string2<<endl;
// string1 = line;
int b=string3.length();
line.replace(spot, b, string3);
//line = string1;

}
outputFile<< line<<endl;
//cout<< line <<"is written"<<endl;
//cin.get();
}
outputFile.close();
}

For ex:
my input file contains:

God, save us 0.000 programmers 0.000

i want the output as

God, save us 1.413 programmers 1.413

but the output is
God, save us 1.413 programmers 0.000

whethe i need to use any loop within a line?
Savage's Avatar
E
C
 
Join Date: Feb 2007
Posts: 1,739
#4: May 30 '07

re: Read/Write file in c++


Yes,u do need to use a do while loop,like:

int spot;
Expand|Select|Wrap|Line Numbers
  1. do{
  2.  
  3.     spot = line.find(string2);
  4.  
  5.     if(spot >= 0)
  6.     {
  7.         cout<<"found at"<<spot<<" the string" <<string2<<endl;
  8.         // string1 = line;
  9.         int b=string3.length();
  10.         line.replace(spot, b, string3);
  11.         //line = string1;
  12.  
  13.     }
  14. }while(spot<=line.length());
Savage
 
Join Date: May 2007
Posts: 11
#5: May 30 '07

re: Read/Write file in c++


Yes,u do need to use a do while loop,like:

int spot;
Expand|Select|Wrap|Line Numbers
  1. do{
  2.  
  3.     spot = line.find(string2);
  4.  
  5.     if(spot >= 0)
  6.     {
  7.         cout<<"found at"<<spot<<" the string" <<string2<<endl;
  8.         // string1 = line;
  9.         int b=string3.length();
  10.         line.replace(spot, b, string3);
  11.         //line = string1;
  12.  
  13.     }
  14. }while(spot<=line.length());
Savage
thx much savage i did the same
Savage's Avatar
E
C
 
Join Date: Feb 2007
Posts: 1,739
#6: May 30 '07

re: Read/Write file in c++


thx much savage i did the same

Glad to help!

Savage
Reply