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

Read/Write file in c++

P: 11
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?
May 29 '07 #1
Share this Question
Share on Google+
5 Replies


Savage
Expert 100+
P: 1,764
Problem is probably here:

inputFile >> a;

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

Try using getline()

Savage
May 29 '07 #2

P: 11
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?
May 30 '07 #3

Savage
Expert 100+
P: 1,764
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
May 30 '07 #4

P: 11
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
May 30 '07 #5

Savage
Expert 100+
P: 1,764
thx much savage i did the same

Glad to help!

Savage
May 30 '07 #6

Post your reply

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