Solved

Debugging error

Posted on 2004-09-09
7
157 Views
Last Modified: 2010-04-15
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;
}
0
Comment
Question by:pkeketso
7 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12013991
>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 ?
0
 
LVL 46

Accepted Solution

by:
Sjef Bosman earned 75 total points
ID: 12014001
Sorry, didn't compile your program, but I can see some mistakes without it:

- in getline, try using max contains 1: the first loop isn't started, c has no value, result is unpredictable; I suppose that the end-conditions of the loop haven't been thoroughly checked
- you aske for only one line from each filename-parameter, there is no loop for every file calling getline
- I suspect that COMMAND NOT FOUND comes from the input you think you have to give:
>> After running it you type in the name of the file eg input.txt.
The program doesn't even read from standard input

Sjef :)
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 12014005
Eh, is it YOUR program?? Or are we analysing someone else's?
0
 
LVL 11

Expert Comment

by:avizit
ID: 12014018
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


0
 
LVL 1

Expert Comment

by:bsnh99
ID: 12033007
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now