-2

My professor gave us the code to get input from a text file. The issue is it will not compile properly for me. I'm not sure where he (or I) went wrong. I have not modified his code in any way and my txt file is in the same directory as the code.

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


int main()
{
     FILE *fp;
     char ch;
     fp = fopen("IronHeelShort.txt", "r");
     printf("Data inside file : ");
     while(1)
     {
         ch = fgetc(fp);
         printf("%c", ch);
         if (ch == EOF)
         break;

     }
     getch();
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
user1798299
  • 173
  • 2
  • 4
  • 12
  • 2
    So what is the error message – Ed Heal Jul 13 '14 at 17:15
  • 1
    You should check that the file opened. `ch` should be an `int`. You should check if `ch` is `EOF` *before* printing `ch`. Your prof is an idiot. What compiler/OS are you using? – ooga Jul 13 '14 at 17:16
  • 2
    _"Won't compile"_ does not mean anything. What is the error message your compiler display? Surely it must blame you for `getch()`? This should give you a hint: http://www.programmingsimplified.com/c/conio.h/getch – sampathsris Jul 13 '14 at 17:17
  • warning: implicit declaration of function 'getch'. undefined reference to getch. ld returned 1 exit status. – user1798299 Jul 13 '14 at 17:30

2 Answers2

2

chshould be an int anyway The function fgetc() will always return an int, to handle all the char values and EOF which is negative. Here reading your file will prematurely end when finding character 0xFF.

For the compiling issue, change getch() into getchar()

Valentin Mercier
  • 5,256
  • 3
  • 26
  • 50
  • Thanks :) I have done the edits. However can you tell me why is EOF not defined to be -1. From my stdio.h file : `#define EOF (-1)` – Valentin Mercier Jul 13 '14 at 17:35
  • After changing ch to int and changing getch() to getchar(). I get segmentation fault(core dumped) after running? – user1798299 Jul 13 '14 at 17:37
  • @ValentinMercier "From my stdio.h file" is not the same as "defined in the standard". I was whining exactly about this kind of sloppiness found in a C++ textbook at my university (they asserted that `wchar_t` is defined as `unsigned short`, which it may or may not be.) - anyway, +1. – The Paramagnetic Croissant Jul 13 '14 at 17:38
  • Off the top of my head, I don't know what POSIX or similar standards require (maybe POSIX states that `EOF` is -1). But the C standard doesn't say anything about `EOF` besides that it should be distinct to every valid character cast to an `unsigned char` and that it should be negative. I don't know if there is any system defining it to something else than -1. – mafso Jul 13 '14 at 17:41
  • Thanks for these enlightening comments :) By the way for the record I have -1 on Mac OS X – Valentin Mercier Jul 13 '14 at 17:42
  • 2
    @user1798299 the segfault can only be due to a failure to open your file. Please verify that it exists and is next to your program. – Valentin Mercier Jul 13 '14 at 17:43
  • @user3477950: On my (all?) Linux machine, `wchar_t` is 4 bytes and holds Unicode code points encoded as UTF-32… So, that's not just sloppiness, it's just sticking to a certain platform (maybe that MS platform being somehow unable to distinguish between UCS-2 and UTF-16?). – mafso Jul 13 '14 at 17:56
  • 1
    @mafso I said "sloppiness" exactly because it's sticking to a certain platform, which results in students being exposed to only part of the truth. That's what I call "sloppy", but maybe it's not the right expression to use (I'm not a native English speaker). – The Paramagnetic Croissant Jul 13 '14 at 18:44
1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main()
{
    FILE *fp;
    char ch;
    fp = fopen("C:/emule/c/0.html", "r");
    printf("Data inside file : ");
    while (1)
    {
        ch = fgetc(fp);
        printf("%c", ch);
        if (ch == EOF)
            break;

    }
    _getch();
}

UPD

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

void main() {
    FILE *input = NULL;
    char c;

    input = fopen("D:/c/text.txt", "rt");
    if (input == NULL) {
        printf("Error opening file");
        scanf("1");
        exit(0);
    }
    while (fscanf(input, "%c", &c) == 1) {
        fprintf(stdout, "%c", c);
    }

    fclose(input);
    scanf("1");
}
Ivan Ivanov
  • 2,076
  • 16
  • 33