FAQ Database Discussion Community


C programming - reading numbers from text file

c,fscanf
I'm trying to make a sort of a database program and ran into a few issues with reading integers from a text file in C. I have the following code: #include <stdio.h> int main(){ int index; FILE * fp; if((fp = fopen("read_file.txt","r+")) == NULL){ perror("Cannot open file"); printf("\nCreating new file...");...

what does fscanf being == 1 do

c,file,input,fscanf
Hey guys there is more to this code obviously but I am just curious as to what this line of code actually does. I know the while loop and such but am new to the fscanf() while (fscanf(input_file, "%s", curr_word) == 1) ...

return permanent pointer to array after saving delimited text file to it

c,arrays,fscanf
i am trying to store a text file looking like this: Headphones-750, TV-1500, Keyboard-1200, Tablet-80, Speakers-400, DVD-250, Streamer-550, Mouse-50; into an array of type def: typedef struct item{ char* name; int price; }item; typedef struct item{ char* name; int product_code; int price; }item; item *getProducts(){ FILE *fp; fp = fopen("machinedata.txt",...

Problems with taking input from stdin - Invalid read/write from valgrind

c,io,stdin,fgets,fscanf
I ran valgrind on a program I'm working on and I'm getting the following errors - I can't figure out why. Any help would be greatly appreciated ahead of time. Let me know if you need any more information ==27928== Invalid read of size 1 ==27928== at 0x4EB9390: __GI___rawmemchr (rawmemchr.S:25)...

Variable is printed properly right before an if else loop, but does not retain that value one in the if else loop

c,if-statement,fscanf
I've written a program that reads an account value from a text file and then assigns these values to a variable for each account. Once this is done, reading from the same text file, an action to be done to each account value is identified in the format "Account# Actiontype...

fscanf() reading string with spaces in formatted lines

c,fscanf
Using this structure: typedef struct sProduct{ int code; char description[40]; int price; }; I want to read a txt file with this format: 1,Vino Malbec,12 where the format is: code,description,price. But I'm having problems to read the description when it has a space. I tried this: fscanf(file,"%d,%[^\n],%d\n",&p.code,&p.description,&p.price); The code is...

While loop through text file stops unexpectedly

c,file,while-loop,integer,fscanf
I am trying to loop through a text file that contains random content. It's current contents are: "13 -35 57 - 23723724 12taste-34the+56rain-bow845" My program should only get the numbers from the file (-35 as a negative number, but not - 23723724 due to the space in between) and no...

Unclear error in list implementation

c,linked-list,fscanf
I wrote a function to get input from text files, the "fileInput" function, and the content of my text file is like 1 2 4 5 2 4 5 6 the part of main function is like: case 7 head=fileInput(head);break; but for some reason, when I choose case 7 to...

reading from a text file using fscanf()

c,arrays,struct,fscanf
I'm having trouble figuring out what went wrong in my code. I think that my while loop didn't read the file correctly, I was trying to print out the customer name but nothing shows up. for example, I have a file like this. Smith 3 Sweater $22.50 Reich 3 Umbrella...

Fscanf, Strings, and Arrays in C? [closed]

c,arrays,string,fscanf
I have a programming assignment. The code is laid out for us, we just have to fill it in. Its been a while since i've touched arrays so i need some help. The program is declaring a char array. It is using fscanf to get strings from a file. The...

C sscanf (fscanf) behaving differently row from row

c,scanf,fscanf,sscanf
I've this snippet of code which should read text from input file and put it in a struct. void load(FILE *fin, struct camion payload[]){ int i=0; char except; char buf[1000]; while (fgets(buf,sizeof(buf),fin)){ except='A'; sscanf(buf,"%i-%i-%i %i %s %c",&payload[i].day,&payload[i]. month,&payload[i].year,&payload[i].nparcels,payload[i].origin,&except); if (except=='E') payload[i].except=1; i++; } } The fgets works as it should...

Trouble using fscanf to read coordinates in C

c,segmentation-fault,coordinates,fscanf
I know similar questions have been asked but none of them seemed to solve my problem. I get Segmentation fault (core dumped) when I run my code. The first line in "data.dat" has the total number of points in the file and the next lines have the point coordinates (in...

fscanf line with condition

c++,fgets,fscanf
my goal is to read in a data file consisting of just one number per line and write the data into a histogram. There are some comments in the file behind # characters. I want to skip these lines. I have started writing: TH1F *hist = new TH1F("hist","",4096, -0.5,4095.5); //TF1...

c fscanf changes string variable?

c,variables,while-loop,fscanf
i'm a beginner in programming and i'm currently learning c. (through harvard edx cs50). But i've come across something that confuses me. So in my while loop i have: char* bword = word fscanf(fp, "%s", word); Where word is a char[46], bword is a char* and fp is the file...

Why do these two methods of counting words differ significantly?

c,regex,text,fscanf,text-parsing
I wrote a program that allows a user to find the number of instances of a word or collection of words in any text file. The user can enter something like this in the command line: $ ./wordCount Mars TripToMars.txt to search for the number of instances of the word...

Converting binary serialization to human-readable serialization

c,printf,fopen,fread,fscanf
I'm converting a program that used to do a binary dump of a struct to file and read that binary struct back in using fread. I want to convert it to creating and reading human readable data files, which of course means I need to format the data and such....

Invalid read of size 4 on fscanf

c,arrays,valgrind,dynamic-arrays,fscanf
Running my program through valgrind, I'm getting an invalid read of size 4 error in the following code (I think at the line where fscanf is called) Important Info: numIntegers is the maximum number of integers that can be read, while numsInFile specifies the amount of integers present in the...

C Programming: Error in program. Won't show Max/Min/Average of files inputted by user

c,file-io,fscanf
I'm attempting to create a code that the user inputs a filename and the program will then find the Min, Max, and Average of the numbers in the file. This is a sample of a file the user would input into the program (the # are comments and would be...

Ignoring return value of fscanf and Segmentation fault

c,segmentation-fault,fopen,compiler-warnings,fscanf
I was wondering how to solve a Core dumped issue on my C code. When I compile it with: g++ -g MatSim.cpp -o MatSim -lstdc++ -O3, I get three warnings, this is one (The other two are similar and are only differentiated by the string variable name): MatSim.cpp: In function...

How to properly use fscanf to store only the needed information in an array?

c,arrays,file,fscanf
I'm trying to write a program in c that reads a mtx file (mtx stands for midi to text, it's actually a txt file) and stores only the information I need (specifically only the note-on/off and pitch-bend messages) here's an example: the information that I need are: the timing (first...

Read data only when it is present

matlab,data,serial-port,fscanf
I try to read data from the COM3 port. I'm using : in = fscanf(s); if(in == 'A') fclose(s); break; end The problem is, when no data is send to the com3 port, the fscanf line will wait for a certain time and than give a timeout. Is there a...

Find out if you're reading int or double using fscanf

c,parsing,int,double,fscanf
I have a text file that I want to read using C. It contains lines in this format: %d %d (%d or %lf). An example of 3 lines: 1 0 44 2 0 84.13 3 1 15.07 But the last number can be either int or double, arbitrary. How can...

Read File: fscanf doesn't read whitespaces?

c,fscanf
I have a problem fetching lines from File Pointer using fscanf. Let's say a want to fetch a line like this: <123324><sport><DESCfddR><spor ds> Fscanf fetch only this part: <123324><sport><DESCfddR><spor Does anybody know how to overcome this problem? Thanks in advance....

how to handle `%` if it is present in a string while reading it using `fscanf()`

c,fscanf
Let's say there is a file a.txt in which each string is a key values pair as <key: value>. But one constraint is that my key could contains character like % too. For example: as given below string : INDIA integer : 2015 ratio %: 20 integer2 : 2016 Now...

Do white spaces take space in txt files?

c,file-io,whitespace,scanf,fscanf
I'm wondering if the white spaces take space in txt files for C. For example if i'm going to take the file pointer back using fseek() or if I am going to fscanf an input from a file do I need to consider white spaces? A Assume a file has...

C/C++ How to convert back int to string (a word)?

c,string,int,fscanf
In C I'm reading a string from a txt file, for example the string "hello", so i have my: F=fopen("text.txt","r"); fscanf(F,"%s\n",string); Now, if i want to convert this string to hex and decimal, i can do something like this: for (i=0; i<strlen(string); i++) { sprintf(st, "%02X", string[i]); //Convert to hex...

How to fscanf from a .csv which is a output of fprintf

c,printf,fscanf
I want to fscanf a csv file which is a output of fprintf, I set the same format but it didn't work, which means when I use that function to fscanf the file I just made, it didn't succuss, even didn't get into the while-loop. So, how to modify it...

Best way to store the first and second word of a text file into two arrays

c,arrays,char,fopen,fscanf
I'm making a code for a program. It would have three text files: -list1.txt having a column of words like: Cat Dog Fish Computers -change.txt having two columns of text like Dog Marbles Computers Store -list2.txt being empty The program would take list.txt, and store its words in the list2.txt,...

C fscanf to read between square brackets

c,file,fscanf,brackets,square
I have a file which includes datas as [name surname] [birthday] [id] when i try this code while(fscanf(file,"%s %s %s",name,bdate,uid) == 3) bdate gets surname] as a value how can i read informations between square brackets. thanks....