#1
  1. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Yerevan, Armenia
    Posts
    224
    Rep Power
    12

    File opening procedures


    I know this might sound stupid but I would like to know what functions I need to use for working with files.
    I need functions that will work with c++, since I use fopen in c but I get errors when I switch to cpp. I also need functions that will work under any compiler.

    Thanks for any help
    -Nar
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Hi,

    Since C is a subset of C++, I'm not sure why your file I/O isn't working. Maybe you should post some simple code demonstrating the problem.(You can check here for an example of C File I/O: http://www.owlnet.rice.edu/~comp320/...otes/tut02-io/)

    In C++ you can use streams for file I/O, but it's not quite as straight forward as giving you some function names. You have to create a pointer and assign it your file name, create an input file stream object with the pointer as the argument, and then use the object to read in the data.

    const* char filename = "C:\\Data\\data1.txt";
    ifstream inFile(filename);

    //check to see if opening the file was successful:
    if(!inFile)
    {
    cout<<"Failed to open file: "<<filename<<endl;
    return 1;
    }

    int number = 0;

    //read in the data until end of file is encountered:
    while(!inFile.eof())
    {
    inFile>>number;

    //do something here with the data you read in before reading in more data.
    }
    Last edited by 7stud; February 23rd, 2003 at 06:00 AM.
  4. #3
  5. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Yerevan, Armenia
    Posts
    224
    Rep Power
    12

    c file proc


    Thanks,

    I used something like
    f = fopen(path to file,"r+t");
    fscanf(f,"%d",number);

    the stuff compiled ok, but after the app ran it gave an exception.
    I use borlan c++ builder 5.0, and when i do the same code in a
    c application it works fine.

    -Nar
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Test the value of f returned by fopen(). If an error occured opening the file, the its value will be NULL. Using a NULL pointer is a leading cause of run-time errors.
  8. #5
  9. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Yerevan, Armenia
    Posts
    224
    Rep Power
    12
    Cleared it uot a bit

    I tested for NULL, I found out it was NULL indeed when i tried

    fopen("c:\autoexec.bat","r");

    however it gave way when i tried to access a file in the
    c:/tc/bin
    direvtory.

    I tried

    chdir("c:");
    fopen("autoexec.bat","r");

    it didn't work again...

    Any ideas
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,645
    Rep Power
    4248
    Double backslashes are your answer, my friend. :)
    fopen("c:\\autoexec.bat","r");

    Also, try this:
    chdir("c:\\");
    fopen("autoexec.bat","r");

    and see if it works.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Hi,

    I think your problem is with your filename

    fopen("c:\autoexec.bat","r");

    A forward slash is an escape character, so you need to use "\\" when you want to denote a "\". My compiler(VC6) will give me a warning for the single "\", but it will compile, and when I run it, I get null for the pointer. This should work:

    fopen("c:\\autoexec.bat","r");
  14. #8
  15. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Yerevan, Armenia
    Posts
    224
    Rep Power
    12
    Thanks everyone!
    -Nar

IMN logo majestic logo threadwatch logo seochat tools logo