Thread: c programming-scanning csv file and assigning variable to it

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  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 09:44 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 09: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, 05:35 AM
  5. Variable assigning Functions
    By emus21 in forum C Programming
    Replies: 4
    Last Post: 11-09-2003, 01:13 AM

Tags for this Thread