Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read a string from a \n delimited file

Tags:

c

stdio

fgets

scanf

I'm trying to read a return delimited file. full of phrases.

I'm trying to put each phrase into a string.

The problem is that when I try to read the file with

fscanf(file,"%50s\n",string);

the string only contains one word. when it bumps with a space it stops reading the string

like image 854
maty_nz Avatar asked Apr 27 '10 03:04

maty_nz


1 Answers

fscanf(file,"%50[^\n]\n",string);
  1. Every character except \n will be consumed by [^\n]

  2. Maximum 0f 50 chars will be consumed (make sure string has space for 51 atleast)

  3. ..\n",string this makes sure that \n is also consumed so that the next call does not just return a null string.

like image 154
N 1.1 Avatar answered Oct 22 '22 11:10

N 1.1