Read comma-separated "quoted" strings from a file

I am new to C programming but have a bit of Java knowledge, so I want to write a program that reads strings stored in a file, possibly several names separated by comma, such as "boy","girl","car" etc. In Java I would use something like, string str[]=str1.split(" ");.

So I came up with several codes each time but none seems to work, here is my most recent code:

fscanf(fp,"%[^\n]",c);

But this essentially prints the whole line till a new line is found. I have also tried using

fscanf(fp,"%[^,]",c);

And if I use gets() it only gets the first string and ignores all others from the first comma.

This didn't give any reasonable output, it rather gave some minute(encoded) characters. Please can anyone help me with how to pick out string values separated by comma and in quotes

Upvotes: 3

Views: 5278

Answers (4)

BLUEPIXY
BLUEPIXY

Reputation: 40145

#include <stdio.h>

int main(void){
    char line[128];
    char word[32];
    FILE *in, *out;
    int line_length;

    in  = fopen("in.txt", "r");
    out = fopen("out.txt", "w");

    while(1==fscanf(in, "%[^\n]%n\n", line, &line_length)){//read one line
        int pos, len;
        for(pos=0;pos < line_length-1 && 1==sscanf(line + pos, "%[^,]%*[,]%n", word, &len);pos+=len){
            fprintf(out, "%s\n", word);
        }
    }
    fclose(out);
    fclose(in);
    return 0;
}
/* output result out.txt
"boy"
"girl"
"car"
...
*/

Upvotes: 0

user2499703
user2499703

Reputation: 11

You can use strtok() function (string.h) to do this task. store the file data in a string of a considerable size. and apply

str = strtok(full_file_string,",");
/* you can save this string in a 2 dimensional array of characters or print it */
while(NULL != str)
{
    str=strtok(NULL,",");
    /*print or save your next word here as you like */
}

for further reference see manpage of strtok. Hope this might help you :)

Upvotes: 1

ila
ila

Reputation: 58

Below example will extract the substring. The format of your fille should be something like:

"boy","girl","car",

Notice that file string should end with ','

  int read_file_with_string_tokens() {

        char * tocken;
        char astring[127];
        int current = 0;
        int limit;


    char *filebuffer = NULL;
    FILE *file = fopen("your/file/path/and/name", "r");
    if (file != NULL) {

        fseek(file, 0L, SEEK_END);
        int f_size = ftell(file);
        fseek(file, 0L, SEEK_SET);

        filebuffer = (char*) malloc(f_size + 2);
        if (filebuffer == NULL) {
            pclose(file);
            free(filebuffer);
            return -1;
        }
        memset(filebuffer, 0, f_size + 2);
        if (fgets(filebuffer, f_size + 1, file) == NULL) {
            fclose(file);
            free(filebuffer);
            return -1;
        }

        fclose(file);


        memset(astring, 0, 127);


        char *result = NULL;

        tocken = strchr(filebuffer, ',');
        while (tocken != NULL) {
            limit = tocken - filebuffer + 1;
            strncpy(astring, &filebuffer[current], limit - current - 1);

            printf("%s" , astring);
            current = limit;
            tocken = strchr(&filebuffer[limit], ',');
            memset(astring, 0, 127);
        }

        free(filebuffer);
    } 

    return 0;
}

Upvotes: 0

cgledezma
cgledezma

Reputation: 622

fscanf doesn't work with regular expressions, but rather with placeholders. So you need to specify the placeholder for what you want to read, and then fscanf will get the next element that matches your pattern. To get what you want one would use something like:

char word[enough_space];
.
.
.
while(fscanf(fp, "\"%s\"", word) != EOF)
{
  //Do something with yout word.
};

Here you will be trying to get a string between to quotes. Note how the placeholder indicates which part of the match should be saved. on successive calls fscanf will get to the next match and so on. When it consumes the whole file it will return EOF.

Upvotes: 0

Related Questions