1

I am trying to figure out why this is not printing, I am trying to print each letter from a text file that is inputted through command prompt, but I am just getting an empty output... What am I doing wrong, and why does this not work? I feel like this logically should work. Thanks.

int main(int argc, char *argv[]) {
    FILE *fp;
    int i;
    for (i = 1; i < argc; i++) {
        printf("%s\n", argv[i]);
        fp = fopen(argv[i], "r");
        while (!feof(fp)) {
            puts(fp);
        }
        fclose(fp);
   }    
   return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 1
    See http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Ed Heal Nov 13 '16 at 18:27
  • 1
    Anyway - `fp` is for reading so `puts` does not make any sense – Ed Heal Nov 13 '16 at 18:28
  • You are not reading from any file `puts(fp)` is nonsense - it's the file pointer. First check that `fp` result of `fopen` is not `NULL`. Then read from the file. But you have as yet nowhere to read into. – Weather Vane Nov 13 '16 at 18:29
  • Please read this [similar question](http://stackoverflow.com/questions/4823177/reading-a-file-character-by-character-in-c). – Weather Vane Nov 13 '16 at 18:33

3 Answers3

1

You are attempting to print a file pointer:

puts(fp);

Read the manual of puts() -that's not what it takes.

To read char-by-char and print on the stdout, you can do:

int ch;
fp = fopen(argv[i], "r");
if (!fp) {
   perror("fopen");
   exit(1);
}
while((ch=fgetc(fp)) != EOF) {
   putchar(ch);
}
flcose(fp);

Unless you are passing multiple file names as arguments, your outer loop doesn't make much sense.

P.P
  • 117,907
  • 20
  • 175
  • 238
  • Oh ok, that makes total sense, thanks. I think I was confusing a file pointer with a char pointer. I am still confused on the difference though: if a file pointer points to the first character in the file, isn't that what a char pointer does too? – Submersed24 Nov 13 '16 at 18:49
  • You also want to read [this](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) to know `while (!feof(fp))` is not what you want in most use-cases. – P.P Nov 13 '16 at 18:50
  • I did just what said and still no output, also, yes, I am testing for the word frequency of multiple files. – Submersed24 Nov 13 '16 at 20:07
  • @Submersed24 May be, `fopen()` failed or you didn't pass arguments or the file is empty? or you have some other logic that's not correct? There are quite a few possibilities as to why there's no output. – P.P Nov 13 '16 at 20:13
  • ... Ya thank you, I am using codeblocks, I didn't realize the arguments don't save – Submersed24 Nov 13 '16 at 20:16
1

Your program has multiple problems:

  • You do not test the return value of fopen(): the program invokes undefined behavior if any of the command line arguments cannot be opened as a stream for reading.

  • while(!feof(fp)) is incorrect. Read this: Why is “while ( !feof (file) )” always wrong?

  • puts(fp); is incorrect as fp is a FILE *, not a string. Use a loop to copy the file contents one byte at a time.

Here is a corrected version:

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

int main(int argc, char *argv[]) {
    FILE *fp;
    int i, c;

    for (i = 1; i < argc; i++) {
        fp = fopen(argv[i], "r");
        if (fp == NULL) {
            fprintf(stderr, "cannot open %s: %s\n", argv[i], strerror(errno));
        } else {
            printf("%s\n", argv[i]);
            while ((c = getc(fp)) != EOF) {
                putchar(c);
            }
            fclose(fp);
        }
   }    
   return 0;
}
Community
  • 1
  • 1
chqrlie
  • 131,814
  • 10
  • 121
  • 189
0
int main(int argc, char *argv[]) {
    FILE *fp;
    int i;
    char buff[128];
    for (i = 1; i < argc; i++) {
        printf("\n%s\n", argv[i]);
        if(NULL == (fp = fopen(argv[i], "r"))){//check open file
            perror("fopen");
            continue;
        }
        while (fgets(buff, sizeof buff, fp)) {//read into buffer
            fputs(buff, stdout);//print buffer (not add newline)
        }
        fclose(fp);
   }    
   return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70