using (importing and saving) csv files

This is a discussion on using (importing and saving) csv files within the C++ Programming forums, part of the General Programming Boards category; Hey all, for my next (and last, about time) assignment, my professor is asking us to try and use/manipulate csv ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    15

    using (importing and saving) csv files

    Hey all, for my next (and last, about time) assignment, my professor is asking us to try and use/manipulate csv files (specifically to import, manipulate, and finally save). This is the first time in the semester that we're playing around with them, and I'm not a bit clueless as to where to begin. Would it be similar to opening and closing regular files? If I already wrote something like this:
    Code:
    if (argc != 3) //if there are an incorrect number of arguments
    	{
    		cerr 	<< "\n\tIncorrect usage.\n"
    		<< "\tCorrect syntax: " << argv[0] << " inputFile outPutFile\n\n";
    		exit(1);
    	}
    	
    	ifstream fin;
    	ofstream fout;
    	
    	fin.open (argv[1]); //opens input file from command line
    	
    	if (fin.fail()) //if file cannot open
    	{
    		cerr << "Could not open " <<argv[1] << endl;
    		exit(1);
    	}
    	
    	fout.open (argv[2]);
    	
    	if (fout.fail()) //if file cannot open
    	{
    		cerr << "Could not open " <<argv[2] << endl;
    		exit(1);
    	}
    //main program, using fin and fout
    
            fin.close();
    	fout.close();
    Would this be able to provide a template for using csv files? Or do I need to learn new syntax? Thanks in advance for your troubles!

  2. #2
    Registered User xentaka's Avatar
    Join Date
    May 2011
    Posts
    60
    Are you referring to comma-separated values (CSV)? If so that should be fairly straightforward.

    I would attack this with find() and substr(), but there is probably a C++ replacement for strtok that I don't know about.
    Last edited by xentaka; 05-20-2011 at 07:16 PM.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    15
    Sorry. Yea, Comma Separate Values. I'm a bit new, so do you mean that whenever I need to read in data, I would use find(), and then whenever I need to modify, I would use substr()? And finally, how would I save the data into a new file (create a new entry/whatever)? Thanks again for any and all help

  4. #4
    Registered User xentaka's Avatar
    Join Date
    May 2011
    Posts
    60

    Well

    I would wait for one of the gurus to speak up on this, but as its simply a text file with a distinct token I would think its gonna be fairly easy. STRTOK was really made for this, but it will not accept C++ strings far as I can tell.

    EDIT: Looked this up and found a code segment that may help you. This is not mine, but it will provide an alternative to strtok for working with strings.

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    std::string myText("some-text-to-tokenize");
    std::istringstream iss(myText);
    std::string token;
    while(getline(iss, token, '-'))
    {
          std::cout << token << std::endl;
    }
    Last edited by xentaka; 05-20-2011 at 07:45 PM.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,147
    O_o

    Within C++, using `std::string', the `strtok' or `strpbrk' functions don't directly exist, but `strcspn' is basically spelled `std::string::find_first_of'.

    Parsing a regular old "CSV" file is, largely, is easy as using `std::getline', `std::string::find_first_of', and `std::string::substr' in a nice little dance.

    See what you can figure out with those functions.

    Parsing the variation that uses escape characters and allows quoted strings with the nested non-consecutive separators is a little more difficult, but you don't have to go there unless you need to.

    Soma

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    There is no split in the standard library (aka strtok), but Boost has some nice string algorithms, filling out the missing parts of the standard library. Among them, a split function.
    Basically something like

    Code:
    std::vector<std::string> vector;
    boost::algo::split(vector, input_string, boost::any_of("-"));
    Probably some wrong here and there, but that's the basics of it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,682
    An easy way of extracting variables from a CSV file is to use getline(stream, std::string, delimiter) in a loop. Then convert the extracted string to the proper type of variable with the stringstream class.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Importing arrays from files
    By Mavix in forum C Programming
    Replies: 3
    Last Post: 05-07-2007, 12:08 PM
  2. Importing DLL / Library Files
    By magic.mike in forum Windows Programming
    Replies: 5
    Last Post: 03-22-2005, 04:47 AM
  3. Replies: 4
    Last Post: 03-19-2005, 03:21 PM
  4. Saving and Importing text files
    By ColdFire in forum Windows Programming
    Replies: 4
    Last Post: 06-17-2002, 08:32 AM
  5. importing program files
    By altoba in forum C Programming
    Replies: 1
    Last Post: 01-21-2002, 11:43 AM

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