fscanf

replace the stdin for a file

Hello, I got a program with a fscanf like this: fscanf(stdin, "%d %d,.... I got many fscanf and files that I'd like to test, the files are like this 10485770 15 51200000 -2 10 10 10485760 10485760 10 10485760 10485760 10 10485760 10485760 Well my question is how can I tell to the program or the compiler to take the inputs not from ...

fscanf reading problem

Hi, sorry, perhaps this is a stupid question. I have a file like this: 36 146 10 53 240 133 104 28 51 81 124 ... so I want to read the numbers from a program, so I do: .... some function ..... int i; unsigned char key[16]; FILE *fp; printf ("\n ------ \n"); // open filename fp = fopen("key.txt","a"); printf("...

How to know new line character in fscanf?

Hi gurus, How to know that fscanf reached a new line \n in a file. I have been using my own functions for doing that. I know I can use fgets and then sscanf for my required pattern. But my requirements are not stable, some times I want to get TAB separated strings, some times new line separated strings and some times some special charac...

Question regarding pointers in fscanf.

I am using C. I am having issues with using pointers for the fscanf function. When I try to do: int *x; /* ... */ fscanf(file, "%d", x[i]); My compiler gives me a warning saying "format argument is not a pointer" and the code just doesn't run (I get a message saying "Water.exe has stopped working"). If I replace x with *x, it just doe...

Read In Hex Values (C)

Hello all. I am currently attempting to read in Hex values from a text file. There can be multiple lines of Hex's and each line can be as long as needed: f53d6d0568c7c7ce 1307a7a1c84058 b41af04b24f3eb83ce Currently, I put together a simple loop to read in the Hex values into an unsigned char line[500] with fscanf as such: ...

What is the difference between these three fscanf calls in OCaml?

I wrote a short bit of code to simply skip num_lines lines in an input file (printing the lines out for debugging purposes. Here's two things I tried that didn't work: for i = 0 to num_lines do print_endline (fscanf infile "%s" (fun p -> p)); done;; for i = 0 to num_lines do print_endline (fscanf infile "%S\n" (fun p -> p)); done;...

retrieving a string with spaces from a file in C

We were given an assignment that involved taking information from a file and storing the data in an array. The data in the file is sorted as follows New York 40 43 N 74 01 W the first 20 characters are the name of the city followed by the latitude and longitude. latitude and longitude should be easy with a few fscanf(i...

Is there a more efficient way of reading a formatted file in C than I have done?

Hi, I need to read in a formatted file that looks something like this. Code: HARK Name: Oscar MRTE: Train etc At the moment my code looks like this. FILE *file; char unneeded[10]; char whitespace[2]; char actual[10]; file = fopen("scannertest.txt","r"); fscanf(file,"%s",unneeded); // this is the identifier and the colon (code:) fs...

fscanf problem with reading in String

Hi, I'm reading in a .txt file. I'm using fscanf to get the data as it is formatted. The line I'm having problems with is this: result = fscanf(fp, "%s", ap->name); This is fine until I have a name with a whitespace eg: St Ives So I use this to read in the white space: result = fscanf(fp, "%[^\n]s", ap->name); However, when I try ...

how to parse URLs in c using sscanf() ?

Hi, this is my c code that reads a list of URLs from a file , and tries to separate the various parts of the URL.. This is just rough parsing , i'm not bothered about special cases.. I guess there is some fault with the sscanf() statement , when i run this , i get " segmentation FAULT" .. and moreover , the full url is being assigned t...

Convert function to read from string instead of file in C

I've been tasked with updating a function which currently reads in a configuration file from disk and populates a structure: static int LoadFromFile(FILE *Stream, ConfigStructure *cs) { int tempInt; ... if ( fscanf( Stream, "Version: %d\n",&tempInt) != 1 ) { printf("Unable to read version number\n"); return 0; } c...

Using fscanf with dynamically allocated buffer.

Hi, I got the following code: char buffer[2047]; int charsRead; do { if(fscanf(file, "%2047[^\n]%n%*c", buffer, &charsRead) == 1) { // Do something } } while (charsRead == 2047); I wanted to convert this code to use dynamically allocated variables so that when calling this code often I won't get heavy memory leakage....

What is the modern equivalent (C++) style for the older (C-like) fscanf method?

What is the best option if I want to "upgrade" old C-code to newer C++ when reading a file with a semicolon delimiter: /* reading in from file C-like: */ fscanf(tFile, "%d", &mypost.nr); /*delimiter ; */ fscanf(tFile, " ;%[^;];", mypost.aftername);/* delimiter ; */ fscanf(tFile, " %[^;]", mypost.forename); /*delimiter ; */ fscanf(tFil...

Reading ASCII numbers using "D" instead of "E" for scientific notation using C

Hello, I have a list of numbers which looks like this: 1.234D+1 or 1.234D-02. I want to read the file using C. The function atof will merely ignore the D and translate only the mantissa. The function fscanf will not accept the format '%10.6e' because it expects an E instead of a D in the exponent. When I ran into this problem in Pytho...

C - how to get fscanf() to determine whether what it read is only digits, and no characters

Imagine I have a csv with and each value is an integer. so the first value is the INTEGER 100. I want fscanf() to read this line, and either tell me it's an integer ONLY, or not. So, it would pass 100 but fail on 100t. What i've been trying to get work is "%d," where the comma is the delimiter of my CSV. so the whole function is fscan...

how to read a string from a \n delimited file

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 ...

How can I make fscanf re-read a line upon a condition being met?

while( fscanf( tracefile, "%s ", opcode ) != EOF ){blah} Occasionally I need to cause fscanf to re-read a line upon a certain condition in my code being met. Is this possible; how would I do that? Thanks. ...

I need to know inside of "while(fscanf != EOF){blah}" if the next fscanf is going to return EOF-- because I need to keep executing portions of the {blah} code after fscanf hits EOF. ?

I've got some code executing in a while(fscanf != EOF) loop. However, even when fscanf has finished executing, I need to keep running that code until some conditions are met. I mean I guess I could copy/paste the code to outside the while(fscanf) loop, and only use global variables, but that seems messy. Surely someone has encountered so...

c read float from file and sort

Hi all I have a problem with a C application; i have on a .txt file some float numbers and I have to read them and sort in descending way. When i do the fscanf command and then the printf, i get on the screen strange numbers (memory location I suppose). How can i solve the problem? Thanks in advance Edited The application is composed by...

How can I create an fscanf format string to accept white space and comma (,) tokenization

I've got some analysis code (myprog) that sucks in data using the following: if(5 == fscanf(in, "%s%lf%f%f%f", tag, & sec, & tgt, & s1, & s2)) which works just fine. But in the situation where I've got data files that are separated by commas, I'm currently doing something like: sed 's/,/ /g' data | myprog Can I modify the format...