1

The code is supposed to read a user-inputted text file name, copy every character into a multidimensional array, then display it with standard output. It compiles, but produces unintelligible text. Am I missing something?

    for (i = 0; i < BIGGEST; i++) {
        for (j = 0; j < BIGGESTL; j++) {
            if (fgetc(array, fp) ) != EOF)
                array[i][j] = c;
            else array[i][j] = '\0'


        }

    fclose(fp);    
    return 0;
}
Robert Penfield
  • 15
  • 1
  • 1
  • 5

3 Answers3

2

You stop filling the array when you encounter EOF, but you print the full array out no matter what.

If the data read from the file is smaller than the input array, you will read that data in and then print that data out, plus whatever random characters were in the memory locations that you do not overwrite with data from the file.

Since the requirement seems to be to print text data, you could insert a special marker in the array (e.g. '\0') to indicate the position where you encountered EOF, and stop displaying data when you reach that marker.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • Also check that you do not exceed the maximum stack size for your platform and/or compiler settings. You allocate nearly 1MB of memory. For VS the default stack size seems to be 1MB: http://stackoverflow.com/questions/1825964/c-c-maximum-stack-size-of-program – Eric J. Oct 14 '13 at 16:15
0

You had better read each line from file

For example:

int i = 0;
while(fgets(text[i],1000,fp))
{
    i++;
}
user2767553
  • 73
  • 1
  • 2
  • 6
0

Though the question is edited and only part of the code is left in question. I am posting more than what is required for the question at the moment. Reason being, there can be numberous improvements to originally posted full code.

In main() function:

You need to check for the argc value to be equal to 2 for your purpose and only then read in value of argv[1] . Else if program executed without the command-line-argument which is file_name in this case, invalid memory read occurs, resulting in segmentation fault if you read in argv[1].

In read_file_and_show_the contents() function:

Stop reading file if end of file is reached or maximum characters is read and store in the character array.

Below Program will help you visualize:

#include <stdio.h>

/*Max number of characters to be read/write from file*/
#define MAX_CHAR_FOR_FILE_OPERATION 1000000 

int read_and_show_the_file(char *filename)
{  
   FILE *fp;
   char text[MAX_CHAR_FOR_FILE_OPERATION];
   int i;

   fp = fopen(filename, "r");

   if(fp == NULL)
   {
      printf("File Pointer is invalid\n");
      return -1;
   }
   //Ensure array write starts from beginning
   i = 0;

   //Read over file contents until either EOF is reached or maximum characters is read and store in character array
   while( (fgets(&text[i++],sizeof(char)+1,fp) != NULL) && (i<MAX_CHAR_FOR_FILE_OPERATION) ) ;

   //Ensure array read starts from beginning
   i = 0;

   while((text[i] != '\0') && (i<MAX_CHAR_FOR_FILE_OPERATION) )
   {
      printf("%c",text[i++]);
   }

   fclose(fp);

   return 0;
}

int main(int argc, char *argv[])
{
   if(argc != 2)
   {
      printf("Execute the program along with file name to be read and printed. \n\
              \rFormat : \"%s <file-name>\"\n",argv[0]);
      return -1;
   }

   char *filename = argv[1];

   if( (read_and_show_the_file(filename)) == 0)
   {
      printf("File Read and Print to stdout is successful\n");
   }
   return 0;
}  
smRaj
  • 1,246
  • 1
  • 9
  • 13