Jump to content

Recommended Posts

Posted

Hey all,

 

Our lecturers have provided us with an assignment where they want us to read in a file they've supplied in a certain file path.

 

They want to be able to type in the file path through a standard input function. I have set the code up but whenever I run my code the program prints out the prompt for input but then skips to the next printf() statement.

 

Here's the code:


location *nav_information;
ship *ship_information; /*instance of the struct in 'resources.h'* pointer needed for dynamically sized array */
time_format *times;
FILE *fp;

fp = fopen("LOGFILE.TXT", "w+"); /*this is the logfile we will write ship information and collision information in */

int duration;
int time_gap;

printf("Welcome to the Shipping Lanes NavBot!\n");

printf("Please enter the data file you wish to use...\n");
fscanf(fp, "%d %d %d %d %d %d", &times->days, &times->months, &times->years, &times->hours, &times->mins, &times->secs);
   	while(!EOF)
   	{
       	fscanf(fp, "%c %f %f %d %d",&ship_information->AISID, &nav_information->lat, &nav_information->lng,	&ship_information->course_over_groud, &ship_information ->speed);
   	}

	printf("Please enter a duration for the simulation...\n");

 

On runtime the printf() message is printed out but I am unable to provide input as it skips to the next printf() function.

 

N.B. I am also getting a warning which says 'statement not reachable relating to the

fscanf()

statement within the while loop.

 

Can anyone provide a solution?

Daniel

Posted (edited)

Just a tip - you might want to store your filename somewhere in memory after typing it in, so that you can actually do stuff with it. Your fp pointer is a filehandle for your log file, which you'll mostly be wanting to write to, not read from. I'm guessing that you have a data file of sorts with the necessary info which is what you'll be fscanf()ing in. It's unlikely you'll be reading data from a file handle which has been opened for writing!

Edited by Nijverheid
Posted

If you want to request user input in C you need to use the scanf() function which you aren't doing in this example. Until you actually request user input the program won't block for IO. The while loop is also unreachable as you aren't actually testing anything, you are only testing the negation of the constant EOF, you need to check this value against something.

 

In short:

  • You have to use scanf() to request user input and put it into a string.
  • You need to check your logic in the while loop as testing for the negation of EOF (which will never be true) won't actually do anything.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.