Troubleshooting Opening and Reading Files in C

  • Thread starter Thread starter Luongo
  • Start date Start date
  • Tags Tags
    Data files
AI Thread Summary
To open the "cargo.dat" file in C, ensure that the file is located in the same directory as your project or specify the full path in your code. If the file is on your desktop in a folder named "apsc 160," the correct path would be "C:\Users\<username>\Desktop\apsc 160\cargo.dat." Alternatively, you can rename the folder to "apsc_160" to avoid issues with spaces in the path. If the program still cannot open the file, check the current working directory in Visual Studio to confirm where the program is looking for the file. Properly managing file paths is essential for successful file operations in C.
Luongo
Messages
119
Reaction score
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;
}
 
Physics news on Phys.org
You can use the full path name for the file, or you can change the current directly of the running program (using the chdir system call).

Also... indent the program. What you've got there hurts. (Though it may just be some kind of weird consequence of cutting and pasting between different environments.)

Cheers -- sylas

PS. Oops. Hurkyl put me right. You DID indent, but the indentation doesn't come through without the code tags. Sorry to doubt you!
 
Last edited:
If you use [code ] ... [/code] tags, it will retain your indentation.
 
sylas said:
You can use the full path name for the file, or you can change the current directly of the running program (using the chdir system call).

Also... indent the program. What you've got there hurts. (Though it may just be some kind of weird consequence of cutting and pasting between different environments.)

Cheers -- sylas

PS. Oops. Hurkyl put me right. You DID indent, but the indentation doesn't come through without the code tags. Sorry to doubt you!


sorry, i don't follow why is it not opening? can u tell me how to open it step by step? my coding is fine what am i supposed to do with the actual cargo.dat file?
 
Luongo said:
sorry, i don't follow why is it not opening? can u tell me how to open it step by step? my coding is fine what am i supposed to do with the actual cargo.dat file?

Probably 'cause the code can't figure out where cargo.dat is. If cargo.dat isn't in the same directory as your C file, you need to either:
Code:
#define DATA_FILE "/path/cargo.dat"
where path is c://full path
you can find the path by right clicking on cargo.dat, selecting properties, and clicking on the details path. (That's in vista, but they all sort of work the same way.)

or you move cargo.dat into your working folder, which should be listed in the visual studio project directory window.
 
story645 said:
Probably 'cause the code can't figure out where cargo.dat is. If cargo.dat isn't in the same directory as your C file, you need to either:
Code:
#define DATA_FILE "/path/cargo.dat"
where path is c://full path
you can find the path by right clicking on cargo.dat, selecting properties, and clicking on the details path. (That's in vista, but they all sort of work the same way.)

or you move cargo.dat into your working folder, which should be listed in the visual studio project directory window.

sorry I am new with this program where is the project directory window? which working folder and where do i find it?
 
Luongo said:
sorry I am new with this program where is the project directory window? which working folder and where do i find it?
It's one of the tabs in the left window pane in visual studio. It's called property manager. You can also just click on the folder at the top of the page while your project is loaded. Clicking it should take you to the directory where the project is saved. Your working folder is the folder in which you are running the code/the folder which you're working out of.

Do yourself a favor and just find the absolute path for cargo.dat. So much easier.
 
story645 said:
It's one of the tabs in the left window pane in visual studio. It's called property manager. You can also just click on the folder at the top of the page while your project is loaded. Clicking it should take you to the directory where the project is saved. Your working folder is the folder in which you are running the code/the folder which you're working out of.

Do yourself a favor and just find the absolute path for cargo.dat. So much easier.


the aboslute path? i put it in a folder on my desktop called apsc 160
what do i put in /apsc 160/cargo.dat?
doesnt work
 
Luongo said:
the aboslute path? i put it in a folder on my desktop called apsc 160
what do i put in /apsc 160/cargo.dat?
doesnt work

no, it's:
Code:
C:\Users\<username>\Desktop\apsc 160\cargo.dat

change apsc 160 to apsc_160, as that sometimes breaks things.
 

Similar threads

Replies
2
Views
2K
Replies
8
Views
2K
Replies
6
Views
2K
Replies
15
Views
3K
Replies
2
Views
4K
Back
Top