Thread: How to add a column of data to a .txt file?

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    13

    How to add a column of data to a .txt file?

    Hello everyone,

    Basically, I have an ASCII file or .txt file with data like this:

    Col1 Col2 Col3....
    5 3
    b 2
    6 1
    7 0

    PS: Sorry, the forum won't display the correct format of the column/line arrangement I have. But they are separate columns with the data under them.

    I want to write data to it in the form of a new column... for example in Col3. I already have my arrays and stuff, it's just a matter of what function/code I need that will help me to do that in an orderly way (data to respective line). I'm a novice in C++ so please help if you know useful functions and use examples please!! Thank you.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You're going to have to write the whole file out again, because (attempting to) write column 3 in place will overwrite the column 1 of the next line. So read in two numbers, write out three (either right away into a temp file and move it over, or read the whole thing into memory then write the whole thing out to memory).

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    Thanks tabstop. What function should I use? So far this is what I have...it's a loop that calculates something (in this case that something is "value"), then that something is what I am trying to add into a new column into the text file. I figured I had to do it in the loop. I'm not sure how creating a copy of the file would work because I don't think I can use my calculated values outside of my while loop.

    Code:
    //while loop to extract data from .txt file
      while(getline(datain, line1))
        {
    	  
          istringstream datstream(line1, istringstream::in);
         
          datstream >> dum1[0] >> dum1[1] /*>> dum1[2] >>*/;
          datastore[i][0] = strtod(dum1[0].c_str(), &pEnd); //lat 
          datastore[i][1] = strtod(dum1[1].c_str(), &pEnd); //long
    	 
    
    	 
          latpix[i] = (laurentides.lat-datastore[i][0])/(laurentides.x_pixsize);
          lonpix[i] = (datastore[i][1]- laurentides.lon)/(laurentides.y_pixsize );
    	
    	  
    
          cout << "Lat : " << datastore[i][0] <<"   Lon: " << datastore[i][1] << "     " << "pixel lat: " << latpix[i] << "\t" << "pixel lon: " << lonpix[i] << endl;
    
          //offset here calculates the  number of pixels that the data occupies in the actual binary file and points to it 
          offset = latpix[i]*laurentides.numsamples+lonpix[i];
          
          //here were getting the size of the data in memory
          offset = offset*sizeof(float);
          cout<<"reading file at "<< offset <<endl;
    
          is.seekg (offset, ios::beg);
    
          
          is.read ((char *) &value[i], sizeof(double));
         
          cout <<"THIS IS FSEEK VALUE: " <<  value[i] << endl;
          ostringstream value_streaming;
    
          
          /* ----Code I was playing with to append data to my text file but not working at all ---
          value_streaming << value[i];
          string_value = value_streaming.str();
     fstream testing;
     testing.open(argv[2], fstream::in | fstream ::out|fstream::app);
          testing << string_value <<  endl;
    
          //string_value = value_streaming.str();
          //value[i] >> dum1[6];
          datstream >> dum1[2] >>  dum1[3] >> dum1[4] >> dum1[5] >> dum1[6];  
          testing.close();
          */
    
          if( i == 4)
    	break;
    	   
          i++;
    	
        }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This perhaps sounds obvious when stated this way, but when you seek to a spot in a file and then write, there aren't little gnomes inside your machine that create a little piece of disk drive just big enough to hold your extra data and then stitch it into place right where you want it; you just write on top of what was already there. In this case, "what was already there" is "the next line of what I was supposed to process". You can either read all the data in, process it, and then write the data out; or if you need/want to write as you go, you must write to a different file.

    I have no idea with this code which you are trying to do (is argv[2] different from where datstream points?), so who knows. If argv[2] is a different place, and you actually bothered to write the whole thing, i.e.
    Code:
    testing << dum1[0] << ' ' << dum1[1] << ' ' << value << std::endl;
    then you'd be fine.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    Ohhh I see. Thanks tabstop. I had a misunderstanding of how the whole thing was compiled. Hm..... argv[2] is basically the file I read from to do the calculations I do and the one I try to write back to.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Blah937 View Post
    Ohhh I see. Thanks tabstop. I had a misunderstanding of how the whole thing was compiled. Hm..... argv[2] is basically the file I read from to do the calculations I do and the one I try to write back to.
    If you have two streams pointing to the same file, then you can get marvelously strange things to happen. In this case, since you open testing in append mode, it can only write to the end of the file, so you aren't going to be stepping on your data. On the other hand, the data won't be where you want it.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    Yeah, i tried it all but the data isn't even being written to the file I want it too; I don't know what's going on.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since there's no way, short of breaking the laws of physics, to make it work that way, you might as well abandon that approach now instead of later. Either decide on (a) a new file or (b) doing it all in memory and then rewriting. If you want to make sure you are reading the data correctly out of your pixel file, you can just print the value to screen to see what you get.

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    Yeah, I'll just create a new file and write on it; I hope it works out for me! Eeeeeeek. Thanks Tabstop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code to search a file and take out a column
    By coletrain in forum C++ Programming
    Replies: 24
    Last Post: 01-27-2009, 04:12 PM
  2. Column count from data file
    By spiit231 in forum C Programming
    Replies: 6
    Last Post: 02-27-2008, 12:59 PM
  3. Adding data to random column in List View
    By Gravedigga in forum Windows Programming
    Replies: 6
    Last Post: 07-25-2004, 07:11 AM
  4. column data read in
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 07-15-2002, 03:39 PM
  5. Writing to a column in a text file
    By olland in forum C Programming
    Replies: 2
    Last Post: 01-21-2002, 06:40 AM