3

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

7
  • "how to read comma separated “quoted” strings from a file in C" - definitely NOT by using scanf()! Commented Jun 19, 2013 at 6:33
  • You can use backticks or highlight the code and press ctrl+k or click near the top and select code. Commented Jun 19, 2013 at 6:34
  • 3
    strchr() and strtok_r() are just fine. Commented Jun 19, 2013 at 6:35
  • @H2CO3 on the contrary, if the input is well formatted then scanf is the solution. The call is made for reading formatted input. Commented Jun 19, 2013 at 7:01
  • @cgledezma But it's hard to get it right, especially for a complete beginner. Commented Jun 19, 2013 at 7:53

4 Answers 4

1

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 :)

Sign up to request clarification or add additional context in comments.

1 Comment

thanks to every who gave help. much thanks. i eventually came up with this and works fine.
0

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.

2 Comments

At least in some implementations (>=C99?) fscanf CAN handle this type of expression (cplusplus.com/reference/cstdio/scanf/?kw=scanf).
@urzeit I guess you're right, but still the solution to reading formatted input would not be to read the whole line and then parse it. It is much simpler to just use fscanf() providing the correct format.
0

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; } 

Comments

0
#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" ... */ 

8 Comments

#include <stdio.h> #include <string.h> int main(void) { FILE *fp; char hello[50]; char *p = hello; char *tokens[50]; int i = 0; fp=fopen("file.txt","r"); fscanf(fp,"%[^\n]",hello); while (i < 50) { tokens[i] = strtok_r(p, " ,", &p); if (tokens[i] == NULL) { break; } i++; } i = 0; while (i < 5) { printf("%s\n", tokens[i++]); } return 0; }
@AdegbitePhilipsMayokun What do you want to say to me?
please how do i rap code to display normally.i have this code which i is suppose to sort string of array using merge sort in c but seems not to work.
@AdegbitePhilipsMayokun Code of your comments may seem to not related to my answer. It what if you another question?
i have gone a bit further, but i am stocked at passing the string array to another function but its not just working. and as much as i can,i have tried pasting code but its not just accepting.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.