0

I am new to coding and using code blocks in windows. But now I need to run a code in the Linux system but I am not getting the output what I am getting on my windows system. Please help to solve the problem.

Input for my code is in a text file that consists of the following inputs:

5
3 0 1 2
3 3 4 5
4 0 1 4 5
4 3 4 1 2
4 3 5 2 6

The code reads the text from the file and prints the output

#include<stdio.h>
#include <stdlib.h>
#define MAXLITTERM 10
#define MAXTERMS 10
int givenSOP[MAXTERMS][MAXLITTERM];
int main()
{

    int column[10];
   char name[] = "input.txt";

   FILE *f1 = fopen("input.txt","r");
   int i,j;
   char S;
   fscanf(f1,"%d",&givenSOP[0][0]);
   printf("\n %d\n", givenSOP[0][0]);

   for(i=1;i<=givenSOP[0][0];i++)
   {
       int point = ftell(f1);

       column[i] = 0;
       S = getc(f1);
       S = getc(f1);
       while(!feof(f1))
       {
           if(S=='\n')
           {
               S = getc(f1);
               break;
           }
           if(S!=' ' && S!='\n')
              column[i]++;
           S = getc(f1);
       }

       fseek(f1,point,SEEK_SET);
       for(j=0;j<column[i];j++)
       {
           fscanf(f1,"%d",&givenSOP[i][j]);
       }
   }

   for(i=1;i<=givenSOP[0][0];i++)
   {
       for(j=0;j<=givenSOP[i][0];j++)
       {
           printf("%d ",givenSOP[i][j]);
       }
       printf("\n");
   }

   return 0;
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 2
    [Please see Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/q/5431941/2173917)"} – Sourav Ghosh Sep 15 '20 at 06:24
  • 1
    Why don't you simply read lines (with [`fgets`](https://en.cppreference.com/w/c/io/fgets)) and then use `sscanf` to parse out the four or five numbers from the line (noticing what `sscanf` [*returns*](https://en.cppreference.com/w/c/io/fscanf#Return_value)). Even if there can be more (or less) numbers in the line, reading the input line by line is still going to be easier to handle than reading one character at a time from the file and jumping back and forth. – Some programmer dude Sep 15 '20 at 06:27
  • 1
    As for your problem of not being able to run in Linux, you probably do something bad leading to *undefined behavior*. Please learn how to *debug* your code, more specifically how to use a *debugger* to step through the code statement by statement while monitoring variables and their values. – Some programmer dude Sep 15 '20 at 06:30
  • 2
    Check if `fopen` succeeds. It might not, because the `input.txt` file might not be there where the program is looking for it. – Jabberwocky Sep 15 '20 at 06:30
  • 1
    `for (i = 1; i <= givenSOP[0][0]; i++)` : array indices start at 0 not at 1. You should probably write `for (i = 0; i < 0 givenSOP[0][0]; i++)` instead. But I'm not sure, I didn't check thoroughly, but this is a very common source of errors. But anyways there might be other problems, I didn't check. – Jabberwocky Sep 15 '20 at 06:36

0 Answers0