-3

I am writing a code which reads only the first line of the text file then outputs it to new text file. I am getting this error named "Segmentation Fault". I cannot understand the reason behind this error.

FILE *inputFile = fopen("input1.txt", "r" );
FILE *outputFile = fopen("output1.txt", "w" );
int i=0;
char line [128][10];
if ( inputFile != '\0' ) {
   while ( fgets ( line[i], sizeof line, inputFile ) != '\0'  ) {
       i++;
       fclose ( inputFile );
   }
}
for(int j=0;j<i;j++) {
    printf("%s",line[j]);
    fprintf(outputFile,"%s",line[j]);
}

Text File Contains 1)Today is Tuesday 2) Tomorrow is Wednesday .... so on (upto 10 lines)

I am trying to only read the 1st line then display that immediately to stdout.Ignoring all other lines below.

holahola
  • 59
  • 3
  • 9
  • shouldn't `while ( fgets ( line[1], sizeof line, inputFile ) != '\0' ){` be `while ( fgets ( line[i], sizeof line, inputFile ) != '\0' ){` – bruceg Feb 22 '18 at 06:14
  • Why are you closing your file in the middle of the loop that reads from your file? Are you copying and pasting code? – MFisherKDX Feb 22 '18 at 06:20

3 Answers3

1

Could be several things, but here are some:

  1. The line Today is Tuesday is at least 17 characters, but your are reading into line[i] which has at most space for 10 characters. You will none the less read sizeof line characters which will be all of the possible characters, because sizeof line for you is 1280 (which is much bigger than 10)
  2. Lets say you successfully read the first line, you then close the input file with fclose ( inputFile ); and try to read from it again. That sounds like undefined behaviour to me.

You need a rubber duck to talk to.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
1

Your explanation and code doesn't match on every detail, but just trying to point out where segmentation fault may have been occurred :

  1. You are trying to read the first line into line[1], which has been allocated 10 character spaces - and you are reading up to sizeof line, which is 128 * 10 = 1280. This already outsizes the buffer you have allocated (10 << 1280). The string "Today is Tuesday" is already more than 10 characters, so you are accessing places you have not meant to, beyond line[1]. (I'm guessing you meant to declare line[10][128])

  2. The while loop continues to run even after you read the first statement, but you have already closed the inputFile in the first loop. You will then be attempting to read from a file that is not even open.

Some debugging would be helpful in pinpointing the exact line in code where the segfault occurred.

But the segmentation fault is just one of the many visible problems, so I recommend you rewrite your code from the beginning. For example, if you are reading just the first line, the while statement itself is unnecessary as well.

wookiekim
  • 1,156
  • 7
  • 20
0

Corrected Code

#include "stdio.h"
int main(void) {
  FILE *inputFile = fopen("input1.txt", "r" );
  FILE *outputFile = fopen("output1.txt", "w" );
  int i=0;
  char line [128][10];
  if ( inputFile )
  {
    while (fgets ( line[i], sizeof line[i], inputFile ) )
    {
      i++;
    }
    fclose ( inputFile );
  }
  for(int j=0;j<i;j++)
   {
     printf("%s",line[j]);
     fprintf(outputFile,"%s",line[j]);
   }
}
Jay Shankar Gupta
  • 5,918
  • 1
  • 10
  • 27
  • While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Ajay Brahmakshatriya Feb 22 '18 at 07:35
  • ok i will give explanation also from next time – Jay Shankar Gupta Feb 22 '18 at 09:19