Thread: Reading external files, (not!!)

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    7

    Question Reading external files, (not!!)

    Hello all,

    I am a complete newbie trying to learn this stuff. I actually have two questions;

    1) I have been looking through a few of the recent threads and I have noticed some differences to my programming, (I'm using MicroSoft Visual Studio 6.0 by the way).

    On this site's programs the header files are:-
    Code:
    #include <iostream>
    #include <fstream>
    On my programming I have to add .h to the header files. Why?

    I'll post the second question on a different message.

  2. #2
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    You should use <iostream> and <fstream> without any .h, because it's the standard library, try using them and adding this line:
    Code:
    using namespace std;
    and it will work fine!

    Note:
    There are many threads dealing with this subject in more details, you might want to check them out!
    none...

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    7

    Question Reading external files (not!!) Part 2.

    I apologise for blathering on but I am really stuck on this problem.

    I have an external file called "test.dat" from a write program. This bit actually works and the following code is supposed to read from it, but it doesn't. Why, please???

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    
    const int max = 80;
    
    int main ()
    {
    ifstream input_file;
    char film[max];
    
    input_file.open("test.dat",ios::in|ios::nocreate);
    
    if (input_file.fail())
    {
    cout << "File does not already exist." << endl;
    exit(1);
    }
    
    while (input_file.peek() != EOF)
    {
    input_file.getline (film, max);
    cout << film <<endl;
    }
    cout << endl;
    
    input_file.close();
    return 0;
    }
    The program compiles OK but when it runs, the result is
    "File does not already exist."

    I am frustrated and confused.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    7
    Thanks very much. I'll try that.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    7
    Thanks ammar.

    I tried that but got 2 probs.

    1. The compiler didn't recognise <stdlib>
    2. When I deleted <stdlib> the compiler didn't recognise
    ios::nocreate. It viewed it as an identifier.

    Sorry fella, I am a REAL newbie here!

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    7
    Salem,

    if you read my reply to Ammar, you know I'm new to this.

    What's "Threads merged" mean?

    I tried adding the test.dat file to the project thinking that it would then be in the right directory. Still the same problem.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    If you put the data file in the project folder, then you can use just the file name, otherwise use the complete path to your data file with double back slashes:

    ifstream inFile("C:\\DataFolder\\test_data.txt")

  8. #8
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Threads merged means that he took the two threads (posts/messages) that you had on this board about similar topics and put them both together under one heading. It has nothing to do with programming, it's just Salem flexing his powers as a mod
    Away.

  9. #9
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    The ios namespace isn't part of the most recent standard. You should use ios_base::in instead. nocreate was removed from the newest standard, too.

    Oh, and if you remove the .h from a standard C library, add c to the front (<stdio.h> becomes <cstdio>).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  10. #10
    Registered User
    Join Date
    May 2003
    Posts
    7
    Originally posted by 7stud
    If you put the data file in the project folder, then you can use just the file name, otherwise use the complete path to your data file with double back slashes:

    ifstream inFile("C:\\DataFolder\\test_data.txt")
    Hi 7stud.

    Thanks for your reply. I tried it but got the same old result---

    "File does not already exist."

    Getting a bit fed up, failing at the first hurdle!!

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Originally posted by ammar
    You should use <iostream> and <fstream> without any .h, because it's the standard library, try using them and adding this line:
    Code:
    using namespace std;
    and it will work fine!

    Note:
    There are many threads dealing with this subject in more details, you might want to check them out!
    that doesn't work with all compilers... i use borland v. 4.5, and i need to use the .h files because it doesn't have the newer libraries...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i use borland v. 4.5, and i need to use the .h files because it doesn't have the newer libraries...
    Get a newer compiler. You're missing out on a lot of new features and there are free compilers for download that comform fairly well to the standard. You should only be using an older compiler to maintain legacy code that won't compile with a newer compiler for some reason.
    My best code is written with the delete key.

  13. #13
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    For example, Dev-C++.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  14. #14
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    i will one day... but I dont' feel like getting my dad's permission to d/l anything (he's the computer nazi and thinks broadband and file-sharing is evil)... eh... i have no problems writing my programs with this one... for now... and the class i'm taking is using this one... i actually have visual studio, but i haven't installed it yet...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i actually have visual studio
    That really isn't much better. I never use Visual Studio for C++, it's just too much of a headache.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. debug to release modes
    By DavidP in forum Game Programming
    Replies: 5
    Last Post: 03-20-2003, 03:01 PM
  2. Unresolved external symbols in OGL
    By Shadow12345 in forum Game Programming
    Replies: 4
    Last Post: 08-04-2002, 09:46 PM
  3. Ask about these "unresolved external symbol" error
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 06-29-2002, 11:39 AM
  4. problem reading files in C
    By angelfly in forum C Programming
    Replies: 9
    Last Post: 10-10-2001, 11:58 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM