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.
Related
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>
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.
I'm trying to locate the strings present in both txt files (one from argv[2], one from stdin), but my loop is only testing the first line of string from the first file with the rest of the second file. I can't seem to figure out why my program doesnt move back into "locations to check against" for each ticket despite having the while loop.
#include <stdio.h>
#include <string.h>
#define BUFLEN (15)
int main(int argc, char *argv[]) {
char buf[15];
char buflocation[6];
char location[6];
FILE *fp = fopen(argv[2],"r");
while (fgets(buf, BUFLEN, stdin) != NULL) {
int i;
for (i = 0; i<4; i++){
location[i] = buf[i];
}
printf("location of ticket we are testing is %s\n",location);
while (fgets(buflocation,sizeof(buflocation),fp) != NULL){
printf("location to check against:%s",buflocation);
if (strncmp(location,buflocation,4) == 0){
printf("this ticket is valid %s\n",buf);
}
}
}
fclose(fp);
return 0;
}
and this is the output
location of ticket we are testing is 1111
location to check against:0101
location to check against:0027
location to check against:1009
location to check against:0077
location to check against:1111
this ticket is valid 111122222220
location of ticket we are testing is 1111
location of ticket we are testing is 9876
location of ticket we are testing is 4526
location of ticket we are testing is 7368
so if my next ticket was 100967789654 then it should come out as valid as well because 1009 is a valid location, but it just doesnt read into that second line. am i not declaring my while statements correctly?
The inner while loop keeps reads the file to the end. At the second iteration of the outer loop the file is already positioned at the end of file. To force rereading, you need to reposition file back to the beginning with:
fseek(fp, 0, SEEK_SET);
when filling your char array you need to terminate it with '\0' character
example:
#include <stdio.h>
#include <string.h>
main()
{
char location[10];
location[0]='a';
location[1]='b';
location[2]='c';
location[3]='\0'; // not adding this is undefined behavior
printf("%s", location);
}
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;
}
I posted a problem yesterday regarding a certain segment of my code. The aim was to basically scan in data values from a .dat file into an array, print the values whilst also counting how many values were in the file.
Sounds pretty simple, but my program only seemed to print a certain number of the values. More specifically, of a data file containing over 300000 values, it would only print the last 20000 and nothing else.
So I left it, finished the rest of my code and now it's the last part I have to sort. I've made a few changes and tried actually printing an output .dat file now so I can see what I'm getting. The code is below by the way.
Initally I assumed perhaps it was something related to the memory allocation of my array (was getting a segmentation error? when putting the whole code together) so I created an external function that counted the number of values instead (that works).
My only problem now is that it still only chooses to print 20000 values and then the rest are 0s. I was thinking perhaps it had something to do with the type but they all contain 7 dps in scientific notation. Here's a sample of some of the values:
8.4730000e+01 1.0024256e+01
8.4740000e+01 8.2065599e+00
8.4750000e+01 8.3354644e+00
8.4760000e+01 8.3379525e+00
8.4770000e+01 9.8741315e+00
8.4780000e+01 9.0966478e+00
8.4790000e+01 9.4760274e+00
8.4800000e+01 7.1199807e+00
8.4810000e+01 7.1990172e+00
Anyone see where I'm going wrong? I'm sorry for the long question, it's just been bugging me for the last day or so and no matter what I change nothing seems to help. Any kind of input would be greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
int count(int);
const char df[]="data_file.dat";
const char of[]="output_file.dat";
int main(int argc, char *argv[])
{
FILE *input, *output;
int i, N;
float *array;
N = count(i);
input = fopen(df, "r");
output = fopen(of, "w");
array = (float*)malloc(N*sizeof(float));
if((input != (FILE*) NULL) && (output != (FILE*) NULL))
{
for(i = 0; i < N; i++)
{
fscanf(input, "%e", &array[i]);
fprintf(output, "%d %e\n", i, array[i]);
}
fclose(input);
fclose(output);
}
else
printf("Input file could not be opened\n");
return(0);
}
int count(int i)
{
FILE *input;
input = fopen(df, "r");
int N = 0;
while (1)
{
i = fgetc(input);
if (i == EOF)
break;
++N;
}
fclose(input);
return(N);
}
Your biggest problem is that count() doesn't count float values; it counts how many characters are in the file. Then you try to loop and call fscanf() more times than there are values in the file. The first times, fscanf() finds a float value and scans it; but once the loop reaches the end of file, fscanf() will be returning an EOF status. It seems possible that fscanf() will set the float value to 0.0 when it returns EOF.
I suggest you rewrite so that you don't try to pre-count the float values. Write a loop that just repeatedly calls fscanf() until it returns an EOF result, then break out of the loop and close the files.
P.S. If you are going to write a function like count(), you should pass in the filename as an argument instead of hard-coding it. And your version of count() takes an integer argument but just ignores the value; instead, just declare a temp variable inside of count().
EDIT: Okay, here is a complete working program to solve this problem.
#include <stdio.h>
int
main(int argc, char **argv)
{
FILE *in_file, *out_file;
unsigned int i;
if (argc != 3)
{
fprintf(stderr, "Usage: this_program_name <input_file> <output_file>\n");
return 1; // error exit with status 1
}
in_file = fopen(argv[1], "r");
if (!in_file)
{
fprintf(stderr, "unable to open input file '%s'\n", argv[1]);
return 1; // error exit with status 1
}
out_file = fopen(argv[2], "w");
if (!out_file)
{
fprintf(stderr, "unable to open output file '%s'\n", argv[2]);
return 1; // error exit with status 1
}
for (i = 0; ; ++i)
{
int result;
float x;
result = fscanf(in_file, "%e", &x);
if (1 != result)
break;
fprintf(out_file, "%d %e\n", i, x);
}
return 0; // successful exit
}
Note that this version doesn't need to allocate a large array; it just needs a single temporary float variable. Maybe your program will need to store all the float values. In that case, write a count() function that uses a loop similar to the above loop, using fscanf() to count float values.
Also note that this program checks for errors after calling fopen() and fscanf().
You are allocating far more floats in memory (N) than you need because your N is the number of characters in the file, not the number of values in it.
Also, how did you determine that there are 300000 values in the file?