Read lines from FILE - c

I would like to know what is the best way to read a lines from files, given I have
a file, that I'm promised that it would be as followed:
type
string table
color
string brown
height
int 120
cost
double 129.90
each time, one word then I would have 2 words.
I know that fscanf returns the value of the numbers of var it scans, and that's
why I have problem here, because one time the line has 1 argument and the next line it would have 2.
always the first line is only a char*, not longer then 10, and then the next is has 3 options..
if it is written an int then the number followed would be an int, as well as if it a double or a string.
thank you.

From the structure of file i think it can be grouped into a struct. And fscanf can be used like:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
typedef struct Node {
char name[SIZE];
char type[SIZE], value[SIZE];
} Node;
int main() {
FILE *pFile = fopen("sample-test.txt", "r");
if(pFile == NULL) {
fprintf(stderr, "Error in reading file\n");
return EXIT_FAILURE;
}
Node nodes[SIZE];
int nRet, nIndex = 0;
// Just to make sure it reads 3 tokens each time
while((nRet = fscanf(pFile, "%s%s%s", nodes[nIndex].name,
nodes[nIndex].type, nodes[nIndex].value) == 3))
nIndex++;
for(int i = 0; i < nIndex; i++)
printf("%s %s %s\n", nodes[i].name, nodes[i].type, nodes[i].value);
return EXIT_SUCCESS;
}
After reading the file, you can check in your structure array to find your desired int, double depending upon the value of name using sscanf as pointed by Some Programmer Dude.

Related

C fgets giving unexpected result

I'm doing Advent of Code 2022, and I'm hitting a strange bug for day 1 (https://adventofcode.com/2022/day/1) which I need help with please.
Currently, I'm trying to parse a text file which has groups of numbers (https://adventofcode.com/2022/day/1/input) and output the results to the screen. Code as follows:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define IN_FILE "input.txt"
#define MAX_LINE_LEN 20480
int main(int argc, char *argv[])
{
// Variable declaration
char line[MAX_LINE_LEN];
int counter = 0;
int elfno = 0;
FILE *fd;
// Create "Key Value pair" struct. Use ints for both vars as we need to perform arithmetic on the values.
struct kv_store
{
int key;
int value;
};
struct kv_store kv[counter];
//struct kv_store kv_total;
// Open file
fd = fopen(IN_FILE, "r");
if (fd == NULL)
{
// File does not exist. Exit
fprintf(stdout, "\n[*]%s not found in current working directory.", IN_FILE);
return -1;
}
// Iterate through file line by line. Place lines into kv[n].value with kv[n].key as the elf number (elfno). Increment elfno once a blank line is found.
while(fgets(line, MAX_LINE_LEN, fd) != NULL)
{
if(strlen(line) == 1)
{
elfno ++;
continue;
}
else
{
kv[counter].key = elfno;
sscanf(line, "%d", &kv[counter].value);
//kv[counter].value = strtol(line, NULL, 0);
counter ++;
continue;
}
}
for(int i=0;i < counter;i++)
{
fprintf(stdout, "Iteration: %d - Elf: %d holds: %d \n", i, kv[i].key, kv[i].value);
}
// TODO: Upon EOF, we can iterate through kv. If elfno for next value in kv hasn't changed, add value to kv_total[elfno].value. If it does change, move on to the next efls values.
// TODO: Once complete, iterate through kv_total and find the largest number, print to screen.
return 0;
}
Everything seems to be working ok, however on the 4th iteration of the while loop which reads the value 6086 from the input, I get a garbage result. I should be seeing Elf: 1 holds: 6086. The remaining iterations give the correct output (sample screenshot below).
Using my very limited knowledge of gdb and debugging, I've looked at the line variable during runtime and can see that the variable is being modified only when line contains the value 6086 partway through the iteration (screenshot below).
Checks carried out so far:
Used a hex editor to confirm there is no rogue encoding within the input file
Tried using strtol instead of of sscanf
Tried to use malloc() and free() to clear the line variable at the end of each iteration
Any help on this would be much appreciated.

there is a easy way to visualize and modify a txt file?

i'm having a little big trouble in c. in particular im not able to save and modify a .txt file on an easy and efficient way.
the fact is: from a file.txt, i have to save all the words on a struct, and after that i will have to do some operations on this, like modify a specific word, a bubble sort, ecc ecc.
Im having problem on how to correctly save all the words in the struct, in the most generic possible way, even if a word from a line of the file is missing.
i mean:
1 line: word1 word2
2 line: word3
3 line: word4 word5
So even if a word is missing, i need to be able to save all this words, leaving something like a missing space in the struct.
the code that im posting is, at the moment, the best i can make with my hands, because i dont have any more ideas about what i should do.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX (10) //<- is 10 because the max numbers of letters in any single word is less than 10
struct word{
char word1[MAX+1]; //<- here im defying a struct with 2 char. this struct will contain the words coming out from the file.
char word2[MAX+1]; //<- max+1 because i have ti give 1 space for the " " or the "/n" case.
};
struct word *file_read(FILE *fp, int *count){
int dim = 2; //<- the int dim is the dimensione
char buf[1024]; //<- a simple buffer
struct word *w;
int conv = 0; //<- another counter that i will use in sscanf
int i;
if(!(w = calloc(dim , sizeof(*w)))){
free(w);
}
while(fgets(buf, sizeof(buf),fp)!= NULL){
conv = sscanf(buf, "%s %s", w->word1, w->word2);
if(conv >= 1){ //if conv doesnt increase, just no print
printf("\n%s ", w[*count].word1);
}
if(conv == 2){ //the same operation
printf("%s", w[*count].word2);
}
i++;
if(*count>= dim){
dim *= 2;
struct word* temp = realloc(w, sizeof(*w)*dim);
if(temp != NULL){
w = temp;
} else{
free(w);
return NULL;
}
(*count)++;
}
}
return w;
}
int main(int argc, char *argv[]){ //<- the file will be passed by argv[1] argument
FILE *fp; //<- im defying the FILE type
fp= fopen(argv[1], "r"); //<- im opening the file passed from argv[1], in reading mode
if(fp == 0){ //<- if file is not loaded, the programm have to stop.
printf("FILE IS NOT LOADED");
return 1;
}
struct word *w; //<- im creating a struct pointer called w
int count= 0;
if(!(w = file_read(fp, &count))){ //<- going to do the reading subroutine
return 0;
}
//AFTER THE READING, I SHOULD BE ABLE TO SAVE ALL THE WORDS IN THE STRUCT
//AND I SHOUL BE ABLE TO DO SOME OPERATIONS, LIKE VISUALIZE IT DIFFERENT WAYS
//DO BUBBLE SORT, QSORT, MODIFY THE WORDS IN THE STRUCT, ECC...
}
so, please, how can i make it works???? thank you everybody.
i hope i've been clear :)
user3386109's suggestion of adding int conv to the word structure is good. Still there are errors:
(*count)++ is out of place, so that it's never reached. Move it to the place of the pointless i++.
In order to not store every line's words in the first structure, change all w-> to w[*count]..

reading from a file and copying it to array: Run-Time Check Failure #2

I am trying to copy 65,536 lines from a file to an int array of the same size using a function.
each line contains four hexadecimal digits.
I also added _CRT_SECURE_NO_WARNINGS in properies => c/c++ => preprocessor definitions because i kept getting warnings because i was using f_gets and not f_gets_s to read from the file.
the error I keep getting now is:
Run-Time Check Failure #2 - Stack around the variable 'temp' was
corrupted.
when trying to print the array I see that all the lines are copied but the last line is copied twice or maybe copied once but is printed twice.
I don't understand what I'm doing wrong.
Thanks for the help.
#include <stdio.h>
#define NUMBER_OF_LINES_MEMO 65536
#define NUMBER_OF_REGISTERS 16
#define CHARS_IN_LINE 5
#define CHARS_IN_IMMEDIATE 5
#define _CRT_SECURE_NO_WARNINGS
void createFromFile(FILE *fPtrReadMemin, int *meminLines){
//create a new array of int numbers named meminLines, with the lines of memin text file
//gets pointers for the file memin and for the array meminLines
FILE *copyCreateFromFile = fPtrReadMemin;
int i = 0;
char temp[CHARS_IN_LINE]; //used for copying to the memory array
int temp2;
while (!feof(copyCreateFromFile))
{
fgets(temp, NUMBER_OF_LINES_MEMO, copyCreateFromFile);
if (strcmp(temp, "")==0)
{
break;
}
temp2 = (int)strtol(temp, NULL, 16);
meminLines[i] = temp2;
printf("%04x\n", temp2);
i++;
}
}
int main(int argc, char* argv[])
{
FILE*fPtrReadMemin;
fPtrReadMemin = fopen(argv[1], "r"); //open Memin to read
int meminLines[NUMBER_OF_LINES_MEMO]; // the memory
if (fPtrReadMemin == NULL) { //check if the files were open correctly
printf("There was error using files\n");
exit(1);
}
createFromFile(fPtrReadMemin, meminLines); //create the memory
system("pause");
fclose(fPtrReadMemin);//close all files
return 0;
}
Your buffer has a length of CHARS_IN_LINE:
char temp[CHARS_IN_LINE]; //used for copying to the memory array
But upon calling fgets you provide a buffer length of NUMBER_OF_LINES_MEMO:
fgets(temp, NUMBER_OF_LINES_MEMO, copyCreateFromFile);
You should provide the actual length of the temp buffer to fgets.
fgets(temp, CHARS_IN_LINE, copyCreateFromFile);
or even better
fgets(temp, sizeof temp, copyCreateFromFile);
Furthermore the length of the lines in your file is not 4 but 5, because fgets appends a \n at the end of the line. Therefore CHARS_IN_LINE should be at least 5.
Not directly related:
You can drop this line:
FILE *copyCreateFromFile = fPtrReadMemin;
and use directly fPtrReadMemin instead of copyCreateFromFile.
Your test of end of file is not correct, you should rather test if fgets returns NULL.
The strcmp is useless, you can drop it.
Overall corrected and simplified function:
void createFromFile(FILE *fPtrReadMemin, int *meminLines) {
//create a new array of int numbers named meminLines, with the lines of memin text file
//gets pointers for the file memin and for the array meminLines
int i = 0;
char temp[100]; // not using CHARS_IN_LINE but 100 which is a reasonable
// maximal file length.
while (fgets(temp, sizeof temp, fPtrReadMemin) != NULL)
{
meminLines[i] = (int)strtol(temp, NULL, 16);
printf("%04x\n", meminLines[i]);
i++;
}
}
And you forgot to include following:
#include <string.h>
#include <stdlib.h>

Struct Memory Allocation from File in C, only two variables work

I have to write a program that will read text from a file, break it up into a struct, validate the sections to a certain criteria, then produce two new files; one with the clean data and one with the errors. So far i am up to the stage of breaking up the data from a file and storing it into a struct but it will only work for the first two variables.
the text is separated by colons and i need to put each section into the variables bellow
an example of the text file
0001:0002:0003:0021:CLS
here is my struct
struct packet{
int source;
int destination;
int type;
int port;
char data[50];
};
Bellow is whatworks fine, however as soon as i add another section to add data to the type variable, the program does not work.
fscanf(inFile, "%[^:]: %[^:]:", records[i].source, records[i].destination);
printf("%d - %s _ %s", i+1, records[i].source, records[i].destination);
However this does not work and i need it to. Well i need to expand upon it.
fscanf(inFile, "%[^:]: %[^:]: %[^:]:", records[i].source, records[i].destination, records[i].type);
printf("%d - %s _ %s _ %s", i+1, records[i].source, records[i].destination, records[i].type);
}
if i printf without inputting anything to the struct it displays null as i would expect because nothing is being stored so im thinking that there is something wrong with the fscanf function. As it works for the first two, i dont think that it is a syntax issue so it must be a memory issue. I have used malloc and realloc but ive gotten confused with it and im sure that i have not done it right.
Full Code Listing
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//declaration of function which will print my name and student number
const char * myname();
//declaration of a function that will prompt a user to enter a file and open it if it exists
int openfile();
struct packet{
int source;
int destination;
int type;
int port;
char data[50];
};
int main()
{
int recordCount = 0;
struct packet *records;
records =malloc(sizeof(struct packet));
// printing the my name and student number via the myname function
printf("%s\n", myname());
//executing the openfile function that will open a function
openfile(recordCount, records);
return 0;
}
const char * myname()
{
const char *x = "*************************\nUSERNAME\nUSER NUMBER\nCONTACT NUMBER\n*************************\n";
return x;
}
int openfile(int rCount, struct packet *records)
{
//file pointer which will keep track of the file being accessed
FILE *inFile ;
//creating variable that will hold what the user has entered for a filename to open
char inFileName[100] = { '\0'};
printf("Please Enter the File to open:");
//getting the users input and storing it into the variable just created
scanf("%s", inFileName);
//if the file does not exist, display an appropriate error message
if ((inFile = fopen(inFileName, "r")) == NULL)
{
printf("Cannot Open File **%s**\n", inFileName) ;
exit(1) ;
}
else {
//if the file does exist, process the data
while(fgets(inFileName, 100, inFile)!=NULL)
{
int i =0;
for (i=0; i<30;i++)
{
fscanf(inFile, "%[^:]: %[^:]: %[^:]:", records[i].source, records[i].destination, records[i].type);
printf("%d - %s _ %s _ %s", i+1, records[i].source, records[i].destination, records[i].type);
}
}
//close the file
fclose(inFile);
return 0;
}
};
You're doing it wrong:
fscanf(inFile, "%[^:]: %[^:]:", records[i].source, records[i].destination);
The %[] conversion specifier is for string, but you're passing the values of integers as if they were character pointers. Undefined behavior!
You should be getting heaps of warnings for this from any modern compiler, i.e. one that validates formatting strings.
There's no point in parsing integers as if they were strings, I don't understand why you're not just doing
fscanf(inFile, "%d:%d", &records[i].source, &records.destination);
for the first case.
Also, do note that it's much better to read in whole lines using fgets(), then parsing the line once read using sscanf(), than trying to combine the two steps with fscanf().
Last, you should check the return value of the conversion call to know how many conversion succeeded.

Reading data from a text file in C?

So I'm pretty new at reading data from a text file in C. I'm used to getting input using scanf or hard coding.
I am trying to learn how to not only read data from a text file but manipulate that data. For example, say a text file called bst.txt had the following information used to perform operations on a binary search tree:
insert 10
insert 13
insert 5
insert 7
insert 20
delete 5
delete 10
....
With that example, I would have the following code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *fptr;
char *charptr;
char temp[50];
fptr = fopen("bst.txt", "r");
while(fgets(temp, 50, fptr) != NULL)
{
charptr = strtok(temp, " ");
while(charptr != NULL)
{
charptr = strtok(NULL, " ");
}
}
return 0;
}
I know that within the first while loop strtok() splits each line in the text file and within the second while loop strtok() splits off when the program recognizes a space, which in this case would separate the operations from the integers.
So my main question is, after, for example, the word "insert" is separated from the integer "10", how do I get the program to continue like this:
if(_____ == "insert")
{
//read integer from input file and call insert function, i.e. insert(10);
}
I need to fill in the blank.
Any help would be greatly appreciated!
If I were doing what you're doing, I would be doing it that way :)
I see a lot of people getting upvoted (not here, I mean on SO generally) for recommending that people use functions like scanf() and strtok() despite the fact that these functions are uniformly considered evil, not just because they're not thread-safe, but because they modify their arguments in ways that are hard to predict, and are a giant pain in the ass to debug.
If you're malloc()ing an input buffer for reading from a file, always make it at least 4kB — that's the smallest page the kernel can give you anyway, so unless you're doing a bazillion stupid little 100-byte malloc()s, you might as well — and don't be afraid to allocate 10x or 100x that if that makes life easy.
So, for these kinds of problems where you're dealing with little text files of input data, here's what you do:
malloc() yourself a fine big buffer that's big enough to slurp in the whole file with buckets and buckets of headroom
open the file, slurp the whole damn thing in with read(), and close it
record how many bytes you read in n_chars (or whatever)
do one pass through the buffer and 1) replace all the newlines with NULs and 2) record the start of each line (occurs after a newline!) into successive positions in a lines array (e.g. char **lines; lines=malloc(n_chars*sizeof(char *)): there can't be more lines than bytes!)
(optional) as you go, advance your start-of-line pointers to skip leading whitespace
(optional) as you go, overwrite trailing whitespace with NULs
keep a count of the lines as you go and save it in n_lines
remember to free() that buffer when you're done with it
Now, what do you have? You have an array of strings that are the lines of your file (optionally with each line stripped of leading and trailing whitespace) and you can do what the hell you like with it.
So what do you do?
Go through the array of lines one-by-one, like this:
for(i=0; i<n_lines; i++) {
if( '\0'==*lines[i] || '#' == *lines[i] )
continue;
// More code
}
Already you have ignored empty lines and lines that start with a "#". Your config file now has comments!
long n;
int len;
for(i=0; i<n_lines; i++) {
if( '\0'==*lines[i] || '#' == *lines[i] )
continue;
// More code
len = strlen("insert");
if( 0== strncmp(lines[i], "insert", len) ) {
n = strtol(lines[i]+len+1, &endp, 10);
// error checking
tree_insert( (int)n );
continue;
}
len = strlen("delete");
if( 0== strncmp(lines[i], "delete", len) ) {
n = strtol(lines[i]+len+1, &endp, 10);
// error checking
tree_delete( (int)n );
}
}
Now, you can probably see 10 ways of making this code better. Me too. How about a struct that contains a keywords and a function pointer to the appropriate tree function?
Other ideas? Knock yourself out!
you can call as follows.For example i have put printf but you can replace your insert/delete function instead that.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *fptr;
char *charptr;
char temp[50];
fptr = fopen("bst.txt", "r");
while(fgets(temp, 50, fptr) != NULL)
{
charptr = strtok(temp, " ");
if(strcmp(charptr,"insert")==0)
{
charptr = strtok(NULL, " ");
printf("insert num %d\n",atoi(charptr));
}
else if(strcmp(charptr,"delete")==0)
{
charptr = strtok(NULL, " ");
printf("delete num %d\n",atoi(charptr));
}
}
return 0;
}
I think the best way to read formatted strings in file is using fscanf, the following example shows how to parse the file. You could store the charptr and value for further operations:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *fptr;
char charptr[50];
int value;
fptr = fopen("bst.txt", "r");
while (fscanf(fptr, "%s%d", charptr, &value) > 0)
{
printf("%s: %d\n", charptr, value);
}
return 0;
}
try this code
int main(){
FILE *fp;
char character[50];
int value;
fptr = fopen("input.txt", "r");
while (fscanf(fp, "%s%d", character, &value) > 0)
{
if(strcmp(character,"insert")==0){
insert(value);//call you function which you want value is 10 or change according to file
}
}
return 0;
}

Resources