Code to search a file and take out a column

This is a discussion on Code to search a file and take out a column within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by iMalc Do you even need to write a program for this! If this is just for a ...

  1. #16
    Registered User
    Join Date
    Jan 2009
    Posts
    6
    Quote Originally Posted by iMalc View Post
    Do you even need to write a program for this!
    If this is just for a one-off, you can just import it into Excel and then copy-n-paste the columns out into other files. (if you have access to Excel)
    Or you could just attach the file to your post here and I'll split it for you.
    Thanks guys for your help, im trying to put something together now, ive just never used some of the stuff used for doing this before.

    And yes I do need a program lol, i have a very large number of files to go through and do this for.

  2. #17
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    Here is something that I have been using, just read everything in with the first function and use the second function to separate the data by column.

    LINE is a global typedef of vector<string> used to store each line of a CSV file, so the very first line will hold the column titles, which is used by the second function to separate the columns.

    Code:
    // Reads the datafile into vector stats
    vector<LINE> readFile(const char *filename)
    {
        string line;
        int pos;
    
        ifstream fin(filename);
    
        if(!fin.is_open())
    	{
    		cout << "Failed to open file" << endl;
    		system("EXIT");
    	}
    
        while(getline(fin,line))
    	{
    		LINE ln;
    
            while( (pos = line.find(',')) >= 0)
    		{
    			string field(line.substr(0,pos));
    			line = line.substr(pos+1);
    			ln.push_back(field);
    		}
    		stats.push_back(ln);
    	}
        return stats;
    }
    
    // Parses the vector into rows of information by column and returns the row
    string GetColData(int row_number, string ColName)
    {
        LINE &columnNames = stats[0];
    
        // find the column with the above column name
        for(int column = 0; column < columnNames.size(); column++)
        {
            if( ColName == columnNames[column] )
            {
                // get data for this column
                LINE &row = stats[row_number];
                return row[column];
            }
        }
        return "";
    }
    I almost forgot, stats is also global, but this was just a temporary fix, in the final prog it is not global:
    Code:
    vector<LINE> stats;

  3. #18
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Quote Originally Posted by m37h0d View Post
    normally i'd agree but for the purposes of the OP who cares?
    ok ok ok - i care. it's a simple modification too.

    also i just remembered that . is the concatenation operator, not +

  4. #19
    Registered User
    Join Date
    Jan 2009
    Posts
    6
    Quote Originally Posted by matsp View Post
    A lot of the "what's best/easiest" depends on EXACTLY what the format is. If the file is simply something like this:
    Code:
    1 2 3
    4 5 6
    then using the method described by Salem will be the simples (and perfectly adequate as long as you don't need to care about "idiot entered rubbish into the file" or "file got destroyed by attempt to copy it that failed"etc) method.

    If you have a file that contains "extra decorations" (e.g. quotes or commas), then you will need to come up with a more advanced method of reading the data, as you need to somehow deal with those "decorations" - the method described by Salem won't work DIRECTLY, but if you know the format, and deal with it appropriately, it can be solved by a method SIMILAR to Salems - again with the provision that if you need to deal with error conditions, it gets a bit harder (handling errors ALWAYS adds more work than just doing the stuff that doesn't deal with errors - in fact, more of the code I write deal with "if(error)..." than the code that actualy does "real work").

    --
    Mats
    Ok i see, so the numbers in my file are in a format like this 0.28403E+17 so ill need to correct for that.. right?

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by coletrain View Post
    Ok i see, so the numbers in my file are in a format like this 0.28403E+17 so ill need to correct for that.. right?
    As long as you are reading the data as floating point, that will be read just fine with operator >>.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by coletrain View Post
    Ok i see, so the numbers in my file are in a format like this 0.28403E+17 so ill need to correct for that.. right?
    Shouldn't that be 2.8403E+16, with non leading 0 and all.?

    And no, if you write to a double it should be able to parse that format.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by King Mir View Post
    Shouldn't that be 2.8403E+16, with non leading 0 and all.?

    And no, if you write to a double it should be able to parse that format.
    It will work with both - it obviously takes one more character to print the 0.xxx number rather than x.xx, but nowadays I'd say file-size isn't particularly important, so unless you can shave of at least 30%...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #23
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    But if they are floats, you wouldn't want to keep passing the file through filters like this, otherwise you might start to accumulate conversion errors.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #24
    Registered User
    Join Date
    Jan 2009
    Posts
    6
    I didn't write the code that comes up with these numbers so im not sure of why they come out that way. I do know that it was written in FORTRAN if that makes a difference.

  10. #25
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by coletrain View Post
    Hey guys, I am needing to make a code that can search through different files(that have 3 columns of numbers in them) and put all of the first columns in one file and all the 2nd in a different one.. etc.

    Ive had some basic C++ classes but im not quite sure how to go about this. Is this even possible in C++? Looking for some pointers to get started.
    assuming input files f1 f2 f3 and colums c1 c2 c3
    output files o1 o2 o3

    Read a line from f1 f2 f3
    write
    f1.c1 f2.c1 f3.c1 to o1
    f1.c2 f2.c2 f3.c2 to o2
    f1.c3 f2.c3 f3.c3 to o3
    Repeat for each line of input files.

Page 2 of 2 FirstFirst 12
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