How to use fscanf to read any character in a string before reaching a tab?
My data file has only 1 line:
123'\t'(Tab)345'\t'Le Duc Huy'\t'567
and I use fscanf as follows:
fscanf(fin,"%d %d %d %[^\t]%s %d",&m,&n,&k,s,&q);
it returns q with the wrong value. Can anyone tell me what made this unsuccessful?
Using fscanf(), you will need a negative character class and length:
fscanf()
char string[32]; if (fscanf(fp, "%31[^\t]", string) != 1) ...error or EOF...
, , q undefined. , fscanf(), , , , , , , , 4 5, , .
q
fscanf fgetc ( ):
fscanf
fgetc
int c; string s = ""; for (;;) { c = fgetc(somefile); if (c == '\t' || c == EOF) break; s += c; // ... }
fscanf() :
fscanf (stream, "[^\t]", output);
, !
char foo[100]; scanf("%s\t", foo);
Clear the space before %[. He is eating your tab. Also, as others have said, this code is unsafe and probably unreliable on input that is not formatted exactly as you would expect. It would be better to use fgetsand then analyze it yourself using strtoland a few for loops.
%[
fgets
strtol
Source: https://habr.com/ru/post/1777694/More articles:error C2143: syntax error: missing ';' before '*' - c ++How to insert a space after a character in a text field, depending on all the characters entered, jQuery? - jqueryIs there a way to disable WCF service debugging in VS 2010? - wcf-client"Video format not supported" on iPhone with YouTube player in WebView - iphoneAlgorithms and methods for non-clustering map functions using OpenLayers - geometryWin32 registry key writable to everyone? - windowsHow can I set the style and positioning of a newly created emacsclient? - emacsAccessing the application context from the BundleContextAware class - springDense border of the current shape - matlabHow to set a form action to open a new window with a specific size? - javascriptAll Articles