Pergunta

Everytime I try to run the code it'll print out the contents of the file, however it will print out a garbage value at the end which I don't know how to get rid of. I am supposed to to store the contents of the file into an array, however I am a bit confused on how to do that???

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

char filePrinter(char*arr)

int main (int argc, char**argv)
{
    char fileArray[150];

    if(argc !=2)
    {
        printf("Invalid Entry. Please Enter name of program followed by input filename\n");
    }

    filePrinter(fileArray);

    return 0;
}

char filePrinter(char*arr)
{
    int i;
    FILE*file;
    i=0;

    file=fopen("assests/room.txt","r");
    if(file == NULL)
    {
        printf("Could not open file\n");
        exit(-1);
    }
    else
    {
        while(0 ==feof(file))
        {
            i=fgetc(file);
            printf("%c", i);
        }
    }
    fclose(file);
    return i;
}

file content:

10x16 ds5 h6,5 g7,8 p3,3
10X16 de4 h5,7 g9,2
10X16 dw6,h2,3 m6,7
10X16 dn3,h2,4 p2,3
10X16 de2 h9,9 m4,5
10X16 dn8 h4,5 g1,1*/
Foi útil?

Solução 3

I think the code should be:

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

void filePrinter(char*arr);  

int main (int argc, char**argv)
{
    char fileArray[150];
    memset(fileArray, 0, sizeof(fileArray));

    if(argc !=2)
    {
        printf("Invalid Entry. Please Enter name of program followed by input filename\n");
    }

    filePrinter(fileArray);

    return 0;
}

void filePrinter(char *arr)
{
    int c = 0, j = 0;
    FILE* file = NULL;

    file=fopen("assests/room.txt","r");
    if(file == NULL)
    {
        printf("Could not open file\n");
        exit(-1);
    }
    else
    {
        while (1)
        {
            c = fgetc(file);
            if (c != EOF)
            {
                  arr[j++] = c;
            }
            else
            {
                break;
            }
        }
    }
    fclose(file);
    return;
}

Outras dicas

feof returns true if the last call to a read operation hit EOF. You'd want to test it after the fgetc call. Or, even better, just check whether fgetc returned the special value EOF.

(A FILE * has an "end-of-file marker" that says whether some read operation has hit EOF. Read operations set the "end-of-file marker" upon hitting EOF. Before you've hit---meaning tried to read past---the end of the file, that "end-of-file marker" is clear.)

Timing is bad than look at the beginning of the loop by feof because EOF occur in fgetc.

replace to

while(EOF!=(i=fgetc(file))){
    printf("%c", i);
}

int filePrinter(char*arr){
    int i = 0, ch;
    FILE*file;

    file=fopen("assests/room.txt","r");
    if(file == NULL) {
        printf("Could not open file\n");
        exit(-1);
    } else {
        while(EOF!=(ch=fgetc(file))) {
            //printf("%c", ch);
            arr[i] = ch; //*arr++ = ch;
            ++i;//i : range check
        }
        arr[i] = '\0';
    }
    fclose(file);
    return i;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top