Troubleshooting Opening and Reading Files in C

  • Thread starter Thread starter Luongo
  • Start date Start date
  • Tags Tags
    Data files
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting file opening and reading issues in a C program, specifically regarding the file "cargo.dat". Participants explore potential reasons for the failure to open the file, including directory issues and the use of absolute paths.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant inquires about how to open the file "cargo.dat" in C, expressing that their code compiles but fails to open the file.
  • Some participants suggest using the full path name for the file or changing the current directory of the running program using the chdir system call.
  • There are repeated requests for step-by-step guidance on how to open the file and where to place it.
  • One participant mentions that the code may not find "cargo.dat" if it is not in the same directory as the C file, proposing to either define the full path or move the file to the working folder.
  • Another participant advises finding the absolute path for "cargo.dat" as a simpler solution.
  • There is a suggestion to rename the folder "apsc 160" to "apsc_160" to avoid potential issues with spaces in the path.

Areas of Agreement / Disagreement

Participants generally agree that the issue likely stems from the file not being in the expected directory, but there is no consensus on the best approach to resolve the problem. Multiple views on how to handle file paths are presented.

Contextual Notes

Participants express uncertainty about the exact directory structure and the implications of using spaces in file paths. There are also mentions of different versions of Windows affecting file path navigation.

Who May Find This Useful

New users of Visual Studio and C programming who are encountering file handling issues may find this discussion helpful.

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 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 15 ·
Replies
15
Views
3K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K