I am quite new to C programming and I am coding this program to read in a file and prints out into a table the automatically aligns itself. Everything works perfectly but I am getting a Valgrind warning that states, conditional jump or move depends on uninitialised value(s). I also need to be able to pass in any .txt file not just a specific .txt file. I know that I can use argv but not sure how to use it in a function than call it in main.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// constant to store the maximum size of the records that can be read from the file
enum
{
// update MAX_SIZE to if the number of rows is more than 1000
MAX_SIZE = 1024
};
// structure to store a record from the file
typedef struct
{
char define[20];
char octal[26];
char description[1000];
} Record;
// string to store the header of the table
char header1[20], header2[20], header3[20];
// function to read the file and return the maximum length of the descriptions read from the file
void readFile(Record rec[], int *size,int *length, int *length1, int *length2)
{
int i = 0;
int maxLength,maxLength1,maxLength2=0;
// open a file for input
FILE *fp = fopen("input2.txt", "r");
// check if the file opened successfully
if (fp == NULL)
{
printf("File could not be opened!");
exit(1);
}
// read the file
// first we read the header of each column from the file
fscanf(fp, "%s %s %s\n", header1, header2, header3);
// now we read each record from the file
while (fscanf(fp, " s ", rec[i].define) == 1)
{
fscanf(fp, " s '%[^']'", rec[i].octal, rec[i].description);
// find the length of the description and update the 'maxLength'
if(strlen(rec[i].description) > maxLength)
{
maxLength = strlen(rec[i].description);
}
else if(strlen(rec[i].octal) > maxLength1)
{
maxLength1 = strlen(rec[i].octal);
}
else if(strlen(rec[i].define) > maxLength2)
{
maxLength2 = strlen(rec[i].define);
}
// increment the value of i
i ;
}
printf(" Length: %d \n", maxLength);
// close the input file
fclose(fp);
// update the number of records read from the file
*size = i;
*length = maxLength;
*length1 = maxLength1;
*length2 = maxLength2;
}
void printTable(Record rec[], int size, int length, int length1, int length2)
{
if(length2 >= 15)
{
for (int i = 0; i < length length1 length2 17; i )
printf("-");
printf("\n| %-*s | %*s | %-*s |\n", length2,header1, length1, header2, length, header3);
for (int i = 0; i < length length1 length2 17; i )
printf("-");
printf("\n");
// print the table data
for (int i = 0; i < size; i )
printf("| %-*s | s | %-*s |\n",length2, rec[i].define, rec[i].octal, length, rec[i].description);
// print the footer
for (int i = 0; i < length length1 length2 17; i )
printf("-");
printf("\n");
}
else
{
printf("length: %d \n", length2);
// print the header of the table
for (int i = 0; i < length length1 length2 10; i )
printf("-");
printf("\n| %-*s | %*s | %-*s |\n", length2,header1, length1, header2, length, header3);
for (int i = 0; i < length length1 length2 10; i )
printf("-");
printf("\n");
// print the table data
for (int i = 0; i < size; i )
printf("| %-*s | %*s | %-*s |\n",length2, rec[i].define, length1, rec[i].octal, length, rec[i].description);
// print the footer
for (int i = 0; i < length length1 length2 10; i )
printf("-");
printf("\n");
}
}
// driver function
int main()
{
// create an array of recors of MAX_SIZE
Record recs[MAX_SIZE];
// initialize size of recs to zero
int size = 0;
int length=0;
int length1=0;
int length2=0;
// call readFile() function to read data from the file and update the size of recs
readFile(recs, &size,&length,&length1,&length2);
// call printTable() function to print the table in a well-formatted manner
printTable(recs, size, length,length1,length2);
return 0;
}
Valgrind warning:
==1821== Memcheck, a memory error detector
==1821== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==1821== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==1821== Command: ./main input2.txt
==1821==
==1821== Conditional jump or move depends on uninitialised value(s)
==1821== at 0x10896F: readFile (main.c:50)
==1821== by 0x108E17: main (main.c:147)
==1821==
==1821== Conditional jump or move depends on uninitialised value(s)
==1821== at 0x1089C2: readFile (main.c:54)
==1821== by 0x108E17: main (main.c:147)
CodePudding user response:
int maxLength,maxLength1,maxLength2=0;
does not initialize maxLength
or maxLength1
.
CodePudding user response:
It's trying to tell you that maxLength
and maxLength1
are uninitialized variables.
You have this line for declaration:
int maxLength,maxLength1,maxLength2=0;
But only maxLength2
is initialized to zero in that statement. The other two variables have undefined values.
Hence, when the code gets to line 50 or line 54 and makes this comparison:
if(strlen(rec[i].description) > maxLength){
It's uncertain what maxLength
was expected to be.
CodePudding user response:
Don't use declarations like this:
int maxLength,maxLength1,maxLength2=0;
it's dangerous and difficult to read because maxLength
and maxLength1
will not be initialized to 0.
Instead declare each variable on a separate line, which is far more readable:
int maxLength = 0;
int maxLength1 = 0;
int maxLength2 = 0;