Link to home
Start Free TrialLog in
Avatar of pkeketso
pkeketso

asked on

Debugging error

I have this program(Text listing) which reads from a file and display the output on the screen(unix environment).TThe program already reads from file but it does not display the whole contents of a file in a screen and also it gives me an error whichs says COMMAND NOT FOUND.What you do is that you compile the program and run it. After running it you type in the name of the file eg input.txt. And that's when the error occurs.

Here is the Program below

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXLINE 2000

int getline(char nl[], int max, FILE *fp)
 {
   int c, i;

   i = 0;
   while(--max > 0 && (c = getc(fp)) != EOF && c != '\n')
 
     nl[i++] = c;
   if (c == '\n')
     nl[i++] = c;
   nl[i] = '\0';
   return i;
  }

main(int argc, char *argv[])
{
    int i,found = 0;
    FILE *fp;
    char line[MAXLINE];
    int c;

    for(i = 1; i < argc; i++)
        {
          fp = fopen(argv[i], "r");
           if(fp == NULL)
                {
                fprintf(stderr, "can't open %s\n", argv[i]);
                exit(EXIT_FAILURE);
                }else {
                   getline(line,MAXLINE,fp);
                 }
               
             // while fgets(argv[i], MAXLINE, fp) != NULL)
             
              printf("%s\n",argv[i]);
            fclose(fp);
           
         }

  return EXIT_SUCCESS;
}
Avatar of sunnycoder
sunnycoder
Flag of India image

>After running it you type in the name of the file eg input.txt. And that's when the error occurs.

you need to give filename on the command line

program <filename>

Also, I see no line in your program which says command not found ... how are you executing your program ?
ASKER CERTIFIED SOLUTION
Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Eh, is it YOUR program?? Or are we analysing someone else's?
Avatar of avizit
avizit

the command not found you may be getting cos the directory where the executable is not in your path

you can try

./a.out  

instead.


also you have

printf("%s\n",argv[i]);  at the end , which just prints the arguments i.e , the input filenames


To simplify your program, you can get rid of the getline function, and just read one char at a time (checking for eof), and printing it out. Then you don't have a bug when tring to show a file with very long lines.