Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

problems with file input

djgaloredjgalore Posts: 56Member
hi there, i am tryin to read a maze from a text file and place 2 players from file player.c onto one screen so that i can play my game. my problem is everytime i try this it tells me segmantation fault. please help.

here is the code:

#include

int main() {
FILE * file;
char inputLine [256];
int i = 1;
int over = 0; // false

// Open a file for reading
file = fopen ("maze.txt" , "r");

// Now read the input lines
do {
// Get a line from the file.
fgets (inputLine , 256 , file);
printf("%s", inputLine);
i++;

if(feof(file)) {
over = 1;
}

} while(!over);

// Done
fclose (file);
}

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    your while loop isn't working. Also check that the file was opened.
    : [code]
    : #include
    :
    : int main() {
    : FILE * file;
    : char inputLine [256];
    : int i = 1;
    : int over = 0; // false
    :
    : // Open a file for reading
    : file = fopen ("maze.txt" , "r");

    : if(file == NULL)
    return 1;
    : // Now read the input lines
    : while( fgets (inputLine , 256 , file) != NULL)
    {
    : printf("%s", inputLine);
    : i++;
    :
    : }
    :
    : // Done
    : fclose (file);
    : }
    :
    [/code]:

Sign In or Register to comment.