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);
}
Related
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.
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>
So I have the txt file from which I need to read the number of students written in that file, and because every student is in separate line, it means that I need to read the number of lines in that document. So I need to:
Print all lines from that document
Write the number of lines from that document.
So, I write this:
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, _TCHAR* Argo[]){
FILE *student;
char brst[255];
student = fopen("student.txt", "r");
while(what kind of condition to put here?)
{
fgetc(brst, 255, (FILE*)student);
printf("%s\n", brst);
}
return 0;
}
Ok, I understand that I can use the same loop for printing and calculating the number of lines, but I can't find any working rule to end the loop. Every rule I tried caused an endless loop. I tried brst != EOF, brst != \0. So, it works fine and print all elements of the document fine, and then it start printing the last line of document without end. So any suggestions? I need to do this homework in C language, and I am using VS 2012 C++ compiler.
OP's code is close but needs to use fgets() rather than fgetc() and use the return value of fgets() to detect when to quit, it will be NULL #Weather Vane. Also add a line counter.
#include <stdio.h>
int main(void) {
FILE *student = fopen("student.txt", "r");
unsigned line_count = 0;
if (student) {
char brst[255];
// fgetc(brst, 255, (FILE*)student);
while (fgets(brst, sizeof brst, student)) {
line_count++;
printf("%u %s", line_count, brst);
}
fclose(student);
}
printf("Line Count %u\n", line_count);
return 0;
}
Try this:
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, _TCHAR* Argo[]){
FILE *student;
char brst[255];
char* result = NULL;
//Ensure file open works, if it doesn't quit
if ((student = fopen("student.txt", "r")) == NULL)
{
printf("Failed to load file\n");
return 1;
}
//Read in the file
for ( (result = fgets( brst, sizeof(brst), student));
!feof(student);
(result = fgets( brst, sizeof(brst), student)) )
{
if ( result == NULL ) break; //I've worked on embedded systems where this actually ment waiting on data, not EOF, so a 'continue' would go here instead of break in that case
printf("%s\n", brst);
}
fclose( student );
return 0;
}
feof() is only true after you've read past the end of the file. Using a for with two identical reads, and feof() on the conditional is a simple way to ensure you read the file as expected.
Use feof() to check for an eof condition.
You are correctly reading the file line-by-line, but use fgets(), not fgetc() - and the cast is not needed.
Then use sscanf() to assign the line data to variables (or some "safe" form of it).
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 have difficulty using malloc and fscanf.
I just want to read a file and print out the result using
I got a segmentation fault error when I executed this code.
I am not sure what I have done wrong. I would be very grateful if someone points out a fix.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]){
char* buffer = (char*)malloc(*argv[1]); // Allocate memory
if(buffer=NULL) // if *argv[1] is not existed, exit the program
exit(1);
int n = 0;
FILE* fp=fopen("file.txt","r"); // Open the file
do {
buffer[n] =fscanf(fp,"%c",buffer); // read from file until it gets EOF
} while(buffer[n] != EOF);
fclose(fp); // Close the file
printf("%s",buffer); // Print the stored string
free(buffer); // Return the memory
return 0;
}
Got it. This:
if(buffer=NULL)
should be this:
if(buffer==NULL)
You're setting buffer to NULL. I'm sure you can see what happens next.
More generally, this code is trying to do several things, and it's full of bugs. You should have tested the different functions separately and worked out those bugs along the way.
This here seems wrong:
char* buffer = (char*)malloc(*argv[1]);
The command line argument is a string, but you want a number. You have to convert the string to a number first.
Another problem: In your loop n is never increased, which is why only the first byte of the buffer is written.
Please find the fixed code and the comments inline:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
// Add the check if *argv[1] does not exist, exit the program
long mem_sz=strtol(argv[1],NULL,10);//Safer to atoi
char* buffer = (char*)malloc(mem_sz); // properly allocate memory
//You missed the == in the NULL check.
if(buffer==NULL)
exit(1);
int n = 0;
FILE* fp=fopen("file.txt","r"); // Open the file
if (fp == NULL)//Check fp too
exit(1);
do
{
buffer[n++]=fscanf(fp,"%c",buffer);
} // read from file until it gets EOF and n++
while(buffer[n-1] != EOF);//Check the last read character
buffer[n]=0;//Put an end of string, so that printf later will work correct
fclose(fp); // Close the file
printf("%s\n",buffer); // Print the stored string
free(buffer); // Return the memory
return 0;
}