c programming-scanning csv file and assigning variable to it

This is a discussion on c programming-scanning csv file and assigning variable to it within the C Programming forums, part of the General Programming Boards category; Could some1 tell me how to scan a csv file and assigined variable to each of the interger scanned in ...

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    2

    c programming-scanning csv file and assigning variable to it

    Could some1 tell me how to scan a csv file and assigined variable to each of the interger scanned in a csv file

    an example of the csv file:
    0001,40,,10

    how do i scan the csv file and assign 0001 to variable student id, the 40 to variable module 01,the 1 without input entered to module02 and 10 to module03

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    It depends. For example, if you know the maximum number of characters in a line, then you can use fgets to read line by line, then say, strtok to parse each line. Of course, things would get more difficult if you have to consider say, optionally quoted strings and commas embedded within strings, but if such is not required, then the approach I described could work.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,673
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    2
    but how do i assign a variable with strtok to the part with no input since strtok ignores it
    Last edited by vabo; 12-26-2013 at 08:44 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Quote Originally Posted by vabo
    but how do i assign a variable with strtok to the part with no input since strtok ignores it
    If you find that to be a problem, then use some alternative method of tokenisation, e.g., loop with strchr.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A simple way to do it would be:

    In pseudo code:
    Code:
    for(each field in the row) {
       while(the next char is not a comma) {
          put the char into the current field
          
          if(the last char was a comma && the current char is a comma) {
             the field is empty, so assign a 0 or '\0' char to that field.
          }
       }
    }
    If you are the one creating the data, I'd put a 0 into any empty field. It looks too much like data was somehow skipped, if a field is simply empty.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-31-2012, 03:08 PM
  2. Scanning file contents to variable
    By Porl in forum C Programming
    Replies: 5
    Last Post: 02-11-2011, 08:15 AM
  3. Replies: 1
    Last Post: 09-22-2009, 08:47 PM
  4. Assigning a name to a variable
    By PAragonxd in forum C++ Programming
    Replies: 14
    Last Post: 12-13-2007, 04:35 AM
  5. Variable assigning Functions
    By emus21 in forum C Programming
    Replies: 4
    Last Post: 11-09-2003, 12:13 AM

Tags for this Thread


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