#include <stdio.h>
#include <stdlib.h>
int main ( int argc, char *argv[] )
{
//sets the number of lines ot be read
char strline[10000];
// checks to see that there are only 2 entries in the argv by checking in argc
if ( argc != 2 )
{
printf( "ERROR. Enter a file name\n", argv[0] );
}
else
{
//opens the file which was entered by the user as read only
FILE *infile = fopen( argv[1], "r");
// covers a miss spelling of a file name or file doesn't exist
if ( infile == 0 )
{
printf( "ERROR. Did you make a mistake in the spelling of the file or the File entered doesn't exist\n" );
}
else
{
// File exists read lines, while not at the end of the file
while (!feof(infile))
{
//Get next line to be printed up to 126 characters per a line
if(fgets(strline, 126, infile))
{
//print the current line (stored in strline)
printf("%s",strline);
}
}
//closes the file
fclose( infile );
return 0;
}
}
}
On the 6th line (comment above) I have stated this is the maximum amount of lines the program can read. I was informed yesterday that this isn’t the case.
Can someone explain to me what the code line actually means?
char strline[10000];
So from what people have being saying what setting it to 128 make more snese (126 for fgets and some room)
char strline[10000]; neans you have allocated a buffer that is 10,000 bytes long:
if you wanted to allocate for 10,000 lines instead you would need something like this:
accessing lines would be to assign to each entry in the array
like when a line is read you would need to allocate a buffer for the line and then point to it from strline