0

i need to read a text from a file and pass it back to the main function and i am getting always 3 characters extra after EOF why is that happening ?

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

int activenw(char *);

void main()
{
    char act_con[50];
    int len,i;
    len=activenw(act_con);
    for(i=0;i<=len;i++)
    {
        printf("%c",act_con[i]);
    }
}

int activenw(char *buff)
{
    char ch="\0";
    FILE *fp;
    int i=0;
    fp=fopen("abc.txt","r");
    if(fp==NULL)
    {
        printf("Error opening a file :");
        exit(0);
    }
    while((ch=fgetc(fp))!=EOF)
    {
        printf("%c",ch);
        *buff=ch;
        buff++;
        i++;
    }
    fclose(fp);
    return i;
}

is this implementation is correct?

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
Naggappan Ramukannan
  • 2,564
  • 9
  • 36
  • 59
  • 2
    void main() is not a good sign. Same with the indenting. Not inclined to do much more than this hint: What type is returned by fgetc()? When you understand why, you will also realize that the type of ch is incorrect. – Randy Howard Mar 15 '13 at 01:23

3 Answers3

1

Within your function, you don't actually null-terminate the string. Unless your "string" will be able to contain NUL characters, you'll be better off treating it as a string rather than a character array. However, that's just general advice, not really related to your specific problem.

First, the return code from fgetc is an int (not char) because it must be able to represent every character plus EOF.

Secondly, "\0" is not a character, it's a character pointer to a string constant. If you want the NUL character, you should be using single quotes, not double quotes.

Lastly, your loop in main to print out the characters: it uses <= which will give you one more character than you want - use < instead.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Yeah but that doesn't matter since he prints with len not %s. –  Mar 15 '13 at 01:25
  • i have terminated the string with '\0' (i.e) in my code *buff='\0'. and i have removed i<=len to i – Naggappan Ramukannan Mar 15 '13 at 01:58
  • @NaggappanRM, you still have `ch` as a `char` when it should be an `int`. But, even with that error, the code you gave in your (now deleted) comment works fine for me, the string hello (from the file) followed by the newline (also in the file). If you dump abc.txt with (if you're on Linux) `od -xcb abc.txt`, you'll probably see the newline there. – paxdiablo Mar 15 '13 at 02:05
  • yes i got it so a new line is always there in a file and that is what i am getting as a extra character . Fine now instead of fgetc , what function i can try or what is the best method to handle with strings – Naggappan Ramukannan Mar 15 '13 at 02:21
  • Well, no, files don't _have_ to have a newline but most sensible text editors will put one there. If you want to read strings from a file, use `fgets`. For example, see http://stackoverflow.com/questions/4023895/how-to-read-string-entered-by-user-in-c/4023921#4023921 which could be modified for a file other than stdin. In any case, I'm not saying don't use `fgetc`, I'm saying use it properly (with an `int ch;` declaration). – paxdiablo Mar 15 '13 at 02:26
  • ok thanks i have understood few things but not all .. well i will try my own for more examples ... i will read more on this thanks all for your help – Naggappan Ramukannan Mar 15 '13 at 02:41
0

Supposing len == 0, how many times will this loop run? for(i=0;i<=len;i++) Is that how you print zero characters?

Can a char represent all the values of an unsigned char? It might have that capability... If it can, can it also represent the negative value EOF? Supposing char is an unsigned type, consider the signedness of (ch=fgetc(fp)). Will (unsigned char) EOF ever equal EOF? fgetc returns int, and it does so because int can represent every unsigned char value, as well as the negative EOF value. ch should obviously be int, so you can correctly distinguish between EOF (non-character) and unsigned char values.

autistic
  • 1
  • 3
  • 35
  • 80
  • so if i should not use fgetc? then what function would be the better one? if i use fscanf(fp,"%c",ch); gives me a warning and segmentation fault(core Dump ) error – Naggappan Ramukannan Mar 15 '13 at 02:12
  • Did I say you shouldn't use `fgetc`? Read it again. That warning and segfault can be expected when `fscanf` without reading the manual. What is the type of `ch`? Which type does `fscanf` expect when you give it the `%c` format specifier? Which manual tells you this? – autistic Mar 15 '13 at 02:29
0

First, change i <= len to i < len so that it doesn't count 1 more character. Second, use a hexdump program to check if your file doesn't have extra new line characters or initial unicode byte order marks.

hdante
  • 7,685
  • 3
  • 31
  • 36