0

I want to input in the name of the file and then display the contents of the file (contains lines of numbers). The code I have used is:

#include <stdio.h>
#include <stdlib.h>

void main(int argc,char *argv[])
{
  FILE *file;
  file = fopen(argv[1],"r");
  char line[100];

  while(!feof(file)){
      fgets(line,100,file);
      puts(line);
  }
fclose(file);
}

When I try to run the program in Code Blocks, it just crashes the program. I tried running in Xcode and I get the message Segmentation fault: 11 and then the program just quits. Can someone please help?

Ok so I tried doing it another way, but still no success:

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

int main(int argc,char *argv[])
{
    FILE *file;
    char line[100];
    file = fopen(argv[1],"r");

    do {
        char c = fgetc(file);
        if (c == EOF) break;
        memset(line,0,100);
        while ((c != '\n') && (c != EOF)) {
            c = fgetc(file);
        }
        printf("%s",line);

    } while (true);
    fclose(file);
}
Landon
  • 21
  • 2
  • 8

2 Answers2

0

Try this: (make sure file to read is on the same directory)

 void main(int argc,char *argv[])
 {
     FILE *file;
     char ch;
     if (argc > 1) {
        file = fopen(argv[1],"r");
         //you can use argv[1] ...
         // check if file is there  ...
         if( file == NULL )
         {
           //opening error...handle it by exit()...
            printf("error...");
         }
         while( ( ch = fgetc(file) ) != EOF ){
             printf("%c",ch);
             exit(0);
         }

        fclose(file);
     }else{
     printf("Not enough args");
     exit(0);
     }
}

OR You can use direct calling to the file without arguments....

#include <stdio.h>
#include <string.h>

main()
{
     FILE *fp;
     char buff[255];

     fp = fopen("input.txt", "r");
     if( fp != NULL ){
         while ( !feof(fp ) ){
         memset(buff, '\0', sizeof( buff) );
         fgets(buff, 255, (FILE*)fp);
         printf("%s", buff );
         }
         fclose(fp);
     }
}

input.txt should be on the same directory of this code file...

Riad
  • 3,822
  • 5
  • 28
  • 39
  • Sorry I'm new to C. I need this to be part go my main function, so can I still use your solution? – Landon Oct 19 '14 at 05:55
  • Yes, you can go with it..just run and check if your *argv[1] is ok ..anyway i am going to write the full program for you... – Riad Oct 19 '14 at 06:15
  • run the above code with command line arguments..with parameters...and let me know what you get.... – Riad Oct 19 '14 at 06:27
  • I get -------------- Build: Debug in testing2 (compiler: GNU GCC Compiler)--------------- g++ -o bin/Debug/testing2 obj/Debug/main.o Undefined symbols for architecture x86_64: "_prinf", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) Process terminated with status 1 (0 minute(s), 0 second(s)) 0 error(s), 0 warning(s) (0 minute(s), 0 second(s)) – Landon Oct 19 '14 at 06:39
  • type mistake..prinf...should be printf() on the very last line...edited. – Riad Oct 19 '14 at 07:02
  • Ok now I get an error with exit(0) - Implicitly declaring library function 'exit' with type 'void (int)_attribute_((noreturn))' – Landon Oct 19 '14 at 09:12
  • 1
    `#include ` for `exit`. – Retired Ninja Oct 19 '14 at 09:20
0

Wrong EOF detection.

The return value of fgets() will inform if any data was read. Checking feof() is too late.

if (file == NULL) Handle_OpenError();

//while(!feof(file)){
  // fgets(line,100,file);
while (fgets(line,100,file) != NULL) { 

  puts(line);
}

Note: fputs(line, stdout); rather than puts(line); to avoid the extra '\n'.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256