Luongo
- 119
- 0
how do i open a file cargo.dat text file on C? i have all the proper coding, but when i compile and run it it tells me that it couldn't open the file. why? how do i put it in the same directory? I am using visual studios 2008 thanks!
#include <stdio.h>
#include <stdlib.h>
#define DATA_FILE "cargo.dat"
int main( void )
{
FILE* inputFile;
double totalWeight = 0.0;
double itemWeight;
inputFile = fopen( DATA_FILE, "r" );
if( inputFile != NULL )
{
while( fscanf( inputFile, "%lf", &itemWeight ) == 1 )
{
totalWeight += itemWeight;
}
printf( "Total cargo weight: %.3f\n", totalWeight );
fclose( inputFile );
}
else
{
printf( "Error opening file %s.\n", DATA_FILE );
}
system( "PAUSE" );
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define DATA_FILE "cargo.dat"
int main( void )
{
FILE* inputFile;
double totalWeight = 0.0;
double itemWeight;
inputFile = fopen( DATA_FILE, "r" );
if( inputFile != NULL )
{
while( fscanf( inputFile, "%lf", &itemWeight ) == 1 )
{
totalWeight += itemWeight;
}
printf( "Total cargo weight: %.3f\n", totalWeight );
fclose( inputFile );
}
else
{
printf( "Error opening file %s.\n", DATA_FILE );
}
system( "PAUSE" );
return 0;
}