4

I am really new to C programming and this is a part of an assignment. I am trying to read a comma separated text file in the format:

 [value1], [value2]

in C and trying to pass them as string and int parameter into a function. I have tried using the sscanf() and even manipulation with fgetc() without much help. The space after the comma is proving to be a problem.

Example:

 2001, 102
 1314, 78
 0410, 910
 ...

Please help me out.

Thank you.

rubber boots
  • 14,924
  • 5
  • 33
  • 44
Souradeep Sinha
  • 121
  • 1
  • 1
  • 10
  • 5
    Post what you have tried. – ani627 Oct 18 '14 at 19:07
  • Possible duplicate - please see this: http://stackoverflow.com/q/12911299/1726419 – yossico Oct 18 '14 at 19:09
  • In fact, you can't possiby. The csv-format may contain quoted commas in the data fields and isn't a simple thing to program as a beginner. If you wouldn't solve the simplest case only, you could use a good library and the examples therein: http://sourceforge.net/projects/libcsv/ – rubber boots Oct 18 '14 at 19:12
  • I'm sorry. I was on it last night and did not save the codes that did not give me proper output. – Souradeep Sinha Oct 18 '14 at 19:13
  • 2
    @SouradeepSinha why don't you give it another try before asking here? If it's an assignement, you are supposed to learn from it ... – rubber boots Oct 18 '14 at 19:16
  • @rubberboots I am not allowed to read from a csv file. The file would have a .dat extension having text values. – Souradeep Sinha Oct 18 '14 at 19:17
  • read the lines of the file with fgets() and extract the words in each line with strtok(). Study the documentation for these functions, e.g.: http://www.cplusplus.com/reference/cstring/strtok/ – rubber boots Oct 18 '14 at 19:19
  • @SouradeepSinha: As mentioned by @rubber boots just give it another try. Also post the actual contents of your `.dat` files. – ani627 Oct 18 '14 at 19:19
  • Thanks @rubberboots for the link. I'll read up now. – Souradeep Sinha Oct 18 '14 at 19:23
  • 2
    This question appears to be off-topic because it is just asks for code, shows not attempt to solve it. – Almo Oct 18 '14 at 19:23
  • @1336087 I haven't been given the .dat file yet. I'm supposed to make one of my own in the given format. The grader will use her own .dat file with all possible test cases. – Souradeep Sinha Oct 18 '14 at 19:24
  • `while(2==fscanf(fp, "%d,%d", &value1, &value2)){ //do stuff}` – BLUEPIXY Oct 18 '14 at 19:46
  • @rubberboots Thank you for the help. Solved it finally. – Souradeep Sinha Oct 18 '14 at 20:09

1 Answers1

3

Thanks to @rubberboots for the help.

#include <stdio.h>
#include <string.h>

void main()
{
    FILE *fp = fopen("user.dat", "r");
    const char s[2] = ", ";
    char *token;
    int i;
    if(fp != NULL)
    {
        char line[20];
        while(fgets(line, sizeof line, fp) != NULL)
        {
            token = strtok(line, s);
            for(i=0;i<2;i++)
            {
                if(i==0)
                {   
                    printf("%s\t",token);
                    token = strtok(NULL,s);
                } else {
                    printf("%d\n",atoi(token));
                }       
            }
        }
        fclose(fp);
    } else {
        perror("user.dat");
    }   
}   

user.dat file:

1000, 76

0095, 81

2910, 178

0001, 1

Output:

1000 76

0095 81

2910 178

0001 1

Souradeep Sinha
  • 121
  • 1
  • 1
  • 10
  • Curious, why print the string version of what was read for the first number and use a number version for the second number? Would not is be more consistent to either print both as strings or both as numbers? – chux - Reinstate Monica Oct 19 '14 at 03:44
  • Assignment woes. It doesn't make much sense to me as well. I guess the professor wanted us to learn and kill two birds with one printf statement. :P – Souradeep Sinha Oct 20 '14 at 02:41