From a .txtfile to another

This is a discussion on From a .txtfile to another within the C++ Programming forums, part of the General Programming Boards category; I am trying to resort a few lines in "Main.txt" to another file, "Main1.txt". The lines look like this in ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    383

    From a .txtfile to another

    I am trying to resort a few lines in "Main.txt" to another file, "Main1.txt".
    The lines look like this in "Main.txt":

    12/03/1999,12/04/1999,1535
    12/03/1999,12/04/1999,1537
    12/03/1999,12/04/1999,1538

    When using this code my output look like this:
    It seems that string/int Two/Three did work but not One
    I beleive it has to do with getline in anyway but cant figure it out really ?


    12/04/1999,1535,2,12/03/1999
    12/04/1999,1537,2,03/1999
    12/04/1999,1538,2,03/1999

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    #include <vector>
    #include <cmath>
    #include <algorithm>
    #include <limits>
    #include <ios>
    #include <cstdio>
    
    using namespace std;
    
    int main()
    {		
    	char Comma;
    	std::string One = "";
    	std::string Two = "";
    	int Three = 0;
    		
    	ofstream NewOutFile;
      	ifstream NewFile ("Main.txt");
    	NewOutFile.open ("Main1.txt");
    	
    		while	(    getline(NewFile, One, ',')   )           		
    		{
    			NewFile >> Two;				
    			NewFile >> Comma;			
    			NewFile >> Three;			
    			NewFile.get();				// read in trailing newline character
    
    			NewOutFile << Two  << ',' << Three  << ',' << One  << "\n";
    		}
    
    return 0;
    }
    Last edited by Coding; 02-19-2008 at 02:31 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Why not read the whole line into a string, and then an inner loop with a stringstream delimited by commas?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    I am not sure how to do that really. I have used this method before and it has worked but it is strange why this is not working now. There has just to be a small mistake somewhere I think...

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I was dreaming up something like:
    Code:
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    
    int main()
    {
       std::ifstream input  ("Main.txt");
       std::ofstream output ("Main1.txt");
       std::string line;
       while ( std::getline(input, line) )
       {
          std::istringstream iss(line);
          std::string start, end;
          int value;
          while ( std::getline(iss, start, ',') &&
                  std::getline(iss, end,   ',') &&
                  iss >> value )
          {
             output << start  << ',' << value << ",2," << end << "\n";
          }
       }
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Yes, this did work great actually. I have never used this method before.
    Thank you for this solution...

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    That is just a smidgen of the power of the standard library, if you want to learn a lot more about that sort of stuff, I would recommend Thinking in C++, free from Bruce Eckel's website

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Great! I will look that up. Thanks for the tip also.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21