problems with file input - Programmers Heaven

Howdy, Stranger!

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

Categories

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.