1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Opening up data files on C.

  1. Jan 23, 2010 #1
    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? im 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;
    }
     
  2. jcsd
  3. Jan 23, 2010 #2

    sylas

    User Avatar
    Science Advisor

    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. :yuck: (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: Jan 23, 2010
  4. Jan 23, 2010 #3

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    If you use [code ] ... [/code] tags, it will retain your indentation.
     
  5. Jan 23, 2010 #4

    sorry, i dont 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?
     
  6. Jan 23, 2010 #5
    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 (Text):
    #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.
     
  7. Jan 23, 2010 #6
    sorry im new with this program where is the project directory window? which working folder and where do i find it?
     
  8. Jan 23, 2010 #7
    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.
     
  9. Jan 23, 2010 #8

    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
     
  10. Jan 23, 2010 #9
    no, it's:
    Code (Text):
    C:\Users\<username>\Desktop\apsc 160\cargo.dat
    change apsc 160 to apsc_160, as that sometimes breaks things.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook