C program doesn't output anything - c

This program is supposed to open a text file, then search for given words in argv. It will search for the words line by line and if it finds one of the given words in that line the program should print it.
This the code I wrote for it:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int existe_mot_cle(char s[1024], int argc, char *argv[])
{
int test = 0;
for (int i = 2; i < argc; i++)
{
if (strstr(s, argv[i]))
test = 1;
break;
}
return test;
}
int open_file(char *argv[], FILE *fp)
{
fp = fopen(argv[1], "a");
}
int main(int argc, char *argv[])
{
FILE *fp;
char s[1024];
if (!open_file(argv, fp))
return 0;
while (fgets(s, 1024, fp))
{
if (existe_mot_cle(s, argc, argv))
printf("%s", s);
}
fclose(fp);
}
The problem is when I run it, nothing happens and I don't know why. I am new to the C language. Can someone give me the solution and explain it please?

You are breaking the for loop right after the first if statement is executed. You should surround it with curly braces:
int existe_mot_cle(char s[1024], int argc, char *argv[])
{
int test = 0;
for (int i = 2; i < argc; i++)
{
if (strstr(s, argv[i])) {
test = 1;
break;
}
}
return test;
}
You can make it simpler and more generic:
bool existe_mot_cle(char s[1024], size_t size, const char *ss[])
{
for (size_t i = 0; i < size; i++) {
if (strstr(s, ss[i]))
return true;
}
return false;
}
Also, your open_file() should return an int, but it is not returning anything. Better remove it from your code since it serves no purpose:
int main(int argc, const char *argv[])
{
if (argc < 3) {
printf("Usage: %s [file] [words]\n", argv[0]);
return 0;
}
const char *filename = argv[1]; // More meaningful
const char **otherarg = argv + 2;
FILE *fp = fopen(filename, "r");
if (!fp) {
printf("Could not open %s.\n", filename);
return 0;
}
char s[1024];
while (fgets(s, sizeof s, fp))
{
if (existe_mot_cle(s, argc-2, otherarg)) // I'm using the second "simpler" version
printf("%s", s);
}
fclose(fp);
}

Related

File I/O Uninitialised Variable Problem - C Program

I have the following code and when I try to run it, I get the following warning:
warning: variable 'myfile' is uninitialized when used here [-Wuninitialized]
myfile = fetch_file(myfile, argc, argv);
note: initialize the variable 'myfile' to silence this warning
FILE *myfile;
I have been trying to find out how to fix the warning but haven't been successful.
#include <stdio.h>
#include <stdlib.h>
#define LINE_SIZE 300
FILE * fetch_file(FILE *myfile, int argc, char *argv[1])
{
if (argc == 1)
{
printf("Error, not enough commandline arguments.");
exit(0);
}
myfile = fopen (argv[1], "r");
if (myfile == NULL)
{
printf("\nNo file named %s was found.", argv[1]);
exit(0);
}
else
{
printf("%s was successfully opened", argv[1]);
}
return myfile;
}
void print_file(FILE *the_file, char *line, int size)
{
int count = 0;
while (fgets(line, size, the_file) != NULL)
{
printf("%s", line);
count++;
}
fclose(the_file);
printf("\nThere are %d lines\n", count);
}
int main (int argc, char *argv[])
{
FILE *myfile;
char line[LINE_SIZE];
myfile = fetch_file(--> myfile <-- , argc, argv); <----------- (warning)
print_file(myfile, line, LINE_SIZE);
return 0;
}
Ps: I'm fairly new to asking questions on this website, so if there is any way I can improve my questions and code, feel free to criticise me...
You want this:
FILE *fetch_file(int argc, char *argv[1])
{
if (argc == 1)
{
printf("Error, not enough commandline arguments.");
exit(0);
}
FILE *myfile = fopen (argv[1], "r");
...
}
int main (int argc, char *argv[])
{
FILE *myfile;
char line[LINE_SIZE];
myfile = fetch_file(argc, argv);
...
}
It is pointless to pass the myfile parameter to fetch_file.
The reason for the warning is that you pass an uninitialized value to a function:
This simple code reproduces this exact problem:
int foo(int bar)
{
bar = 2;
return bar * 2;
}
int main (int argc, char *argv[])
{
int kwork; // kwork is not initialized
foo(kwork); // here you pass an uninitialized value
// which the foo function cannot use in a useful manner
}

reading file`s lines char by char into char** array

I wrote the next function that tries to read and enter each line from text file into a string array in c :
int main(int argc,char* argv[])
{
char ** lines;
readFile(argv[1],lines);
}
int readFile(char* filePath,char** lines)
{
char file_char;
int letter_in_line=0;
int line=1;
char* line_string=malloc(1024);
int j=1;
int fd=open(filePath,O_RDONLY);
if (fd < 0)
{
return 0;
}
while (read(fd,&file_char,1) >0)
{
if(file_char != '\n' && file_char != '0x0')
{
line_string[letter_in_line] = file_char;
letter_in_line++;
}
else
{
if(lines != NULL)
{
lines=(char**)realloc(lines,sizeof(char*)*line);
}
else
{
lines=(char**)malloc(sizeof(char*));
}
char* line_s_copy=strdup(line_string);
lines[line-1]=line_s_copy;
line++;
letter_in_line=0;
memset(line_string,0,strlen(line_string));
}
j++;
}
printf("cell 0 : %s",lines[0]);
return 1;
}
I have 2 questions :
1)Whenever the code reaches the print of cell 0, I'm getting
Segmentation fault (core dumped) error. What is wrong ?
2)In case I
want to see the changes in the lines array in my main, I should pass
&lines to the func and get char*** lines as an argument ? In
addition, I will need to replace every 'line' keyword with '*line' ?
*I know that I can use fopen,fget, etc... I decided to implement it in this way for a reason.
There is many issues that make your code core dump.
Here a version very similar to your code. I hope it will help you to understand this.
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
int read_file(const char *filename, char ***result)
{
/* open the file */
const int fd = open(filename, O_RDONLY);
if (fd < 0) {
*result = NULL;
return -1;
}
/* read the file characters by characters */
char *buffer = (char *)malloc(sizeof(char) * 1024);
char c;
int column = 0;
int line = 0;
*result = NULL;
/* for each characters in the file */
while (read(fd, &c, 1) > 0) {
/* check for end of line */
if (c != '\n' && c != 0 && column < 1024 - 1)
buffer[column++] = c;
else {
/* string are null terminated in C */
buffer[column] = 0;
column = 0;
/* alloc memory for this line in result */
*result = (char **)realloc(*result, sizeof(char *) *
(line + 1));
/* duplicate buffer and store it in result */
(*result)[line++] = strdup(buffer);
}
}
free(buffer);
return line;
}
int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "usage: %s [filename]", argv[0]);
return 1;
}
char **lines;
int line_count = read_file(argv[1], &lines);
if (line_count < 0) {
fprintf(stderr, "cannot open file %s\n", argv[1]);
return 1;
}
for(int i=0; i < line_count; i++)
printf("%s\n", lines[i]);
return 0;
}
Here an other version:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int read_file(const char *filename, char ***result)
{
/* init result */
*result = NULL;
/* open the file */
FILE *file = fopen(filename, "r");
if (file == NULL)
return -1;
/* read the file line by line */
char *buffer = (char *)malloc(sizeof(char) * 1024);
int line = 0;
while (fgets(buffer, 1024, file)) {
*result = (char **)realloc(*result, sizeof(char *) *
(line + 1));
(*result)[line++] = strdup(buffer);
}
free(buffer);
return line;
}
int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "usage: %s [filename]", argv[0]);
return 1;
}
char **lines;
int line_count = read_file(argv[1], &lines);
if (line_count < 0) {
fprintf(stderr, "cannot open file %s\n", argv[1]);
return 1;
}
for(int i=0; i < line_count; i++)
printf("%s\n", lines[i]);
return 0;
}

Seg fault from within my print function after reading in a file in c

I have an unknown segfault within my print function when I call it in main and I can't see what the obvious fix is. I have put printf's throughout the program and it doesn't print 'here4' making me think it's due to my print function, or when I call it in main.
I want to read a dictionary file into an array of strings.
Here is a snippet of the code:
Any ideas would be greatly appreciated, thanks.
#define PRIME 1009
void fileRead(int argc, char **argv)
void printTable(int arrayLength, char **table);
int main(int argc, char **argv)
{
char **table;
FILE *fp;
int i, arrayLength = PRIME;
/* Initial memory allocation */
table = (char**)malloc(PRIME*sizeof(char));
fileRead(argc, argv);
printf("here3\n");
for(i = 0; i < arrayLength; i++) {
printTable(arrayLength,table);
}
printf("here4\n");
return 0;
}
void fileRead(int argc, char **argv)
{
FILE *fp;
char *word;
int arrayLength = PRIME;
word = calloc(MAXCHAR, sizeof(char));
fp = fopen (argv[1], "r");
printf("here1\n");
/*read in grid and move along a cell each time */
while (fscanf(fp, "%s", word)!= EOF) {
if (argc != (2)) {
fprintf(stderr, "Cannot open file, %s\n Try again e.g. %s dictionary.txt\n" , argv[1], argv[0]);
}
if(fp == NULL) {
fprintf(stderr, "Cannot open file, %s\n Try again e.g. %s dictionary.txt\n" , argv[1], argv[0]);
return;
}
if (fp == NULL) {
fprintf(stderr, "Error opening file, try file name dictionary.txt\n");
exit(EXIT_FAILURE);
}
}
printf("here2\n");
fclose(fp);
return;
}
void printTable(int arrayLength, char **table)
{
int i;
for(i = 0; i < arrayLength; i++) {
printf("%s\n", table[i]);
}
printf("\n");
}
Let me summarize your code:
you allocate uninitialized memory for table
You call a function fileRead():
Allocate some memory for word
read the file
Do nothing with the data read.
fileRead() does nothing useful: It does not return anything, it doesn't touch table, is vulnerable to a buffer overflow of word and leaves the memory leak of word behind.
And then you printf the unchanged and uninitialized content of table
try this
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PRIME 1009
#define MAXCHAR 256
char **fileRead(FILE *fp, int *len);
void printTable(int arrayLength, char **table);
int main(int argc, char **argv){
if (argc != 2) {
fprintf(stderr, "Need dictionary file argument.\nTry again e.g. %s dictionary.txt\n" , argv[0]);
exit(EXIT_FAILURE);
}
FILE *fp = fopen (argv[1], "r");
if(fp == NULL) {
fprintf(stderr, "Cannot open file, %s\nTry again e.g. %s dictionary.txt\n" , argv[1], argv[0]);
exit(EXIT_FAILURE);
}
int arrayLength = PRIME;
char **table = fileRead(fp, &arrayLength);//fclose(fp) inside this
printTable(arrayLength, table);
for(int i = 0; i < arrayLength; ++i)
free(table[i]);
free(table);
return 0;
}
char **fileRead(FILE *fp, int *len){
char *word = calloc(MAXCHAR, sizeof(char));
char **table = malloc(*len * sizeof(char*));
int i = 0;
while (i < *len && fscanf(fp, "%s", word) != EOF){
table[i] = malloc(strlen(word)+1);
strcpy(table[i++], word);
}
fclose(fp);
*len = i;
free(word);
return table;
}
void printTable(int arrayLength, char **table){
int i;
for(i = 0; i < arrayLength; i++) {
printf("%s\n", table[i]);
}
printf("\n");
}

Unexpected Segfault - What am I doing wrong

I've been trying to blow the cobwebs off my C programming skills, and I've been getting an error I can't seem to figure out. This program reads in a list of integers separated by newlines. This bit happens in read_integer_file... I have no issues going through the input there. It's when I pass the data back to main via out that I have the problem.
#include <stdlib.h>
#include <stdio.h>
int read_integer_file(char* filename, int* out)
{
FILE* file;
file = fopen(filename, "r");
/* check if the file open was successful */
if(file == NULL)
{
return 0;
}
int num_lines = 0;
/* first check how many lines there are in the file */
while(!feof(file))
{
fscanf(file, "%i\n");
num_lines++;
}
/* seek to the beginning of the file*/
rewind(file);
out = malloc(sizeof(int)*num_lines);
if(out == NULL)
return 0;
int inp = 0;
int i = 0;
while(!feof(file))
{
fscanf(file, "%i\n", &inp);
out[i] = inp;
printf("%i\n", out[i]); /* <---- Prints fine here! */
i++;
}
return num_lines;
}
int main(int argc, char** argv)
{
if(argc < 2)
{
printf("Not enough arguments!");
return -1;
}
/* get the input filename from the command line */
char* array_filename = argv[1];
int* numbers = NULL;
int number_count = read_integer_file(array_filename, numbers);
for(int i = 0; i < number_count; i++)
{
/* Segfault HERE */
printf("%i\n", numbers[i]);
}
}
You have not allocated any memory for numbers. Currently it is pointing to no where. When it gets back to the calling function it is still pointed to nowhere. Pass a pointer to a pointer to the function to allocate it within the function.
int read_integer_file(char* filename, int** out)
{
...
*out = malloc(sizeof(int)*num_lines);
...
int number_count = read_integer_file(array_filename, &numbers);
This is a version of your code working.. Keep in mind also that fscanf just skip the \n the way you wrote it so it's like writing fscanf(file, "%d");
And if you don't put a variable to handle what it reads the compiler may not see it but you'll probably get an error..
So here is the code :
#include <stdlib.h>
#include <stdio.h>
int read_integer_file(char* filename, int **out)
{
FILE* file;
file = fopen(filename, "r");
/* check if the file open was successful */
if(file == NULL)
{
return 0;
}
int num_lines = 0;
int garbi;
char garbc;
/* first check how many lines there are in the file */
while(!feof(file))
{
fscanf(file, "%d", &garbi);
fscanf(file, "%c", &garbc);
if (garbc=='\n') ++num_lines;
}
/* seek to the beginning of the file*/
rewind(file);
int *nbr = malloc(sizeof(int)*num_lines);
if(nbr == NULL)
return 0;
int i = 0;
while(!feof(file))
{
fscanf(file, "%d", &nbr[i++]);
fscanf(file, "%c", &garbc);
}
*out=nbr;
return num_lines;
}
int main(int argc, char** argv)
{
if(argc < 2)
{
printf("Not enough arguments!");
return -1;
}
/* get the input filename from the command line */
char* array_filename = argv[1];
int *numbers = NULL;
int number_count = read_integer_file(array_filename, &numbers);
int i;
for(i = 0; i < number_count; ++i)
printf("%d\n", numbers[i]);
return 0;
}

To read the content of a file in C

I have a C code to read a txt file:
#include <stdio.h>
int main()
{
FILE *pf;
int ii;
int jj;
char *filename;
printf("enter file name");
scanf("%s",filename);
printf("%s",filename);
pf = fopen("filename+.txt", "r");
if(pf==Null)
{
printf("cant open");
}
else
{
fscanf(pf,"%d,%d" ,&ii,&jj );
printf("%d,%d\n" ,ii,jj);
}
fclose(pf);
return 0;
}
Still i get segmentation error.
The input txt file contains
2,3
I get segmentation fault(core dumped) when i run the program as ./readfile input.
What is going wrong here , how can i correct this?
int main(char *) is not a legal signature for main in C. Only
int main(void)
and
int main(int argc, char **argv)
are legal. In your case, you will need the latter.
That is not the correct way to specify arguments to your program. ie you can't do this:
int main(char *filename)
There should have been a compiler error when you compiled your program. The correct definition is:
int main( int argc, char **argv )
Where argv is an array of strings. Try doing this experiment:
int main( int argc, char **argv )
{
int i;
for( i = 0; i < argc; i++ ) {
printf( "arg %d is: \"%s\"\n", argv[i] );
}
return 0;
}
Then, write your program to use the correct argument list as above.
One other point to make is that you should test the return value of fopen. If it is NULL, then you should NOT try to access the file (because it failed to open).
There are many mistakes here.
After calling fopen(), you should check if pf is NULL, because fopen() can fail.
You are trying to open the file of name filename+.txt. Shouldn't you be opening the file which the name was provided as parameter?
Also, the structure of main() should be int main(int argc, char **argv), you cannot do whatever you want about this.
Check if argc > 1, in which case the program was started with parameters, and the file name should've been provided in argv[1].
Update on comments: This is how your code should look like:
int main()
{
char filename[512]; // reserve 512 bytes to receive the file name from input
FILE *pf;
int ii;
int jj;
printf("Enter file name: ");
scanf("%s", filename);
pf = fopen(filename, "r");
if (pf)
{
fscanf(pf,"%d,%d", &ii, &jj);
printf("%d,%d\n", ii, jj);
fclose(pf);
}
else
{
printf("Failed to open file name %s", filename);
}
return 0;
}
You can also do this to get the filename from the parameters:
int main(int argc, char **argv)
{
FILE *pf;
int ii;
int jj;
if (argc > 1)
{
pf = fopen(argv[1], "r");
if (pf)
{
fscanf(pf, "%d,%d", &ii, &jj);
printf("%d,%d\n", ii, jj);
fclose(pf);
}
else
{
printf("Failed to open file name %s", argv[1]);
}
}
else
{
printf("Insuficient parameters");
}
return 0;
}
Or even, if you don't want to pass the file extension:
int main(int argc, char **argv)
{
char *filename;
FILE *pf;
int ii;
int jj;
if (argc > 1)
{
filename = malloc(strlen(argv[1]) + 5); // alloc necessary memory
strcpy(filename, argv[1]);
strcat(filename, ".txt");
pf = fopen(filename, "r");
if (pf)
{
fscanf(pf, "%d,%d", &ii, &jj);
printf("%d,%d\n", ii, jj);
fclose(pf);
}
else
{
printf("Failed to open file name %s", filename);
}
}
else
{
printf("Insuficient parameters");
}
return 0;
}

Resources