Thread: Strings and File I/O

  1. #1
    ! |-| /-\ +3 1337 Yawgmoth's Avatar
    Join Date
    Dec 2002
    Posts
    187

    Strings and File I/O

    Here's some code I have:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <fstream.h>
    #include <string.h>
    using namespace std;
    
    int main()
    {
    string Path;
    cout << "Enter path of file to be read: ";
    cin >> Path;
    cout << "\nHere are the contents of that file:\n";
    ifstream fin(Path);
    char ch;
    while(fin.get(ch))
    cout << ch;
    
    cout << "\n\nEnd of file!\n";
    fin.close();
    
    
          system("PAUSE");
          return 0;
    }
    When I tried to compile it on Dev-C++ 4, it gave me the error message: 'string' undeclared (first use this function).

    When I tried to compile it on Codewarrior 6, I got this error message:
    Error : function call 'basic_ifstream(std::basic_string<char, std::char_traits<char>, std::allocator<char>>)' does not match
    'std::basic_ifstream<char, std::char_traits<char>>::basic_ifstream()'
    'std::basic_ifstream<char, std::char_traits<char>>::basic_ifstream(const char *, std::ios_base::openmode)'
    'std::basic_ifstream<char, std::char_traits<char>>::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char>> &)'
    hello.cpp line 13 ifstream fin(Path);



    What do I do?
    L33t sp3@k sux0rz (uZ it t@k3s 10 m1|\|ut3s 2 tr@nzl@te 1 \/\/0rd & th3n j00 h@\/3 2 g3t p@$t d@ m1zpelli|\|gz, @tr0(i0u$ gr@mm@r @|\|d 1n(0/\/\pr3#3|\|$1bl3 $l@|\|g. 1t p\/\/33nz j00!!

    Speling is my faverit sujekt

    I am a signature virus. Add me to your signature so that I may multiply.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    First,wrong header,string.h is a C header,not the C++ std::string header.
    Try
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    #include <string>
    using namespace std;
    and....
    Code:
    ifstream fin(Path.c_str());

  3. #3
    ! |-| /-\ +3 1337 Yawgmoth's Avatar
    Join Date
    Dec 2002
    Posts
    187
    Thanks, all my problems are gone now.
    L33t sp3@k sux0rz (uZ it t@k3s 10 m1|\|ut3s 2 tr@nzl@te 1 \/\/0rd & th3n j00 h@\/3 2 g3t p@$t d@ m1zpelli|\|gz, @tr0(i0u$ gr@mm@r @|\|d 1n(0/\/\pr3#3|\|$1bl3 $l@|\|g. 1t p\/\/33nz j00!!

    Speling is my faverit sujekt

    I am a signature virus. Add me to your signature so that I may multiply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. I/O with strings
    By kristentx in forum C++ Programming
    Replies: 1
    Last Post: 09-13-2007, 02:18 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. File I/O and Strings
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-25-2002, 10:02 PM