Read/Write file in c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sheriff
    New Member
    • May 2007
    • 11

    Read/Write file in c++

    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.........(I n 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



    [CODE=c]#include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;

    int main()
    {
    string a;
    string string1;
    string string2 = "1.413";
    string string3 = "0.000";

    fstream inputFile("C:\\ TEMP\\samp1.txt ");
    fstream outputFile;
    outputFile.open ("C:\\TEMP\\sam p2.txt");

    while (!inputFile.eof ()) {
    inputFile >> a;
    //cout << a ;
    int spot = a.find(string2) ;

    if (spot >= 0) {
    cout << "found at" << spot << " the string" << string2 << endl;
    string1 = a;
    int b = string3.length( );
    string1.replace (spot, b, string3);
    a = string1;
    cout << a << endl;
    cin.get();
    }
    outputFile << a << endl;

    }
    outputFile.clos e();
    }
    [/CODE]
    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?
    Last edited by AdrianH; May 29 '07, 03:16 PM. Reason: INDENT YOUR CODE!
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Problem is probably here:

    inputFile >> a;

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

    Try using getline()

    Savage

    Comment

    • sheriff
      New Member
      • May 2007
      • 11

      #3
      Originally posted by Savage
      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\\sam p2.txt");
      while(!inputFil e.eof())
      {
      //inputFile >> a;
      getline(inputFi le, line);
      cout << line <<endl;

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

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

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

      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?

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

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

        int spot;
        Code:
        do{
        
        	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;
        
        	}
        }while(spot<=line.length());
        Savage

        Comment

        • sheriff
          New Member
          • May 2007
          • 11

          #5
          Originally posted by Savage
          Yes,u do need to use a do while loop,like:

          int spot;
          Code:
          do{
          
          	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;
          
          	}
          }while(spot<=line.length());
          Savage
          thx much savage i did the same

          Comment

          • Savage
            Recognized Expert Top Contributor
            • Feb 2007
            • 1759

            #6
            Originally posted by sheriff
            thx much savage i did the same

            Glad to help!

            Savage

            Comment

            Working...