how to get all texts from a file by ifstream ?

This is a discussion on how to get all texts from a file by ifstream ? within the C++ Programming forums, part of the General Programming Boards category; Code: char *str4 = new char[50]; ifstream Inputfile(argv[0],ios::in); Inputfile.getline(str4, '\n'); sock.Send(num, str4); Inputfile.close(); delete str4; It only return one line ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    13

    Unhappy how to get all texts from a file by ifstream ?

    Code:
     
    
    char *str4 = new char[50];
    ifstream Inputfile(argv[0],ios::in);
    Inputfile.getline(str4, '\n');
    sock.Send(num, str4);
    Inputfile.close();
    delete str4;
    It only return one line
    It should return all texts from a text file.


    Im using ms vc++ 6.0
    the code is cpp non-mfc
    Mouse

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    one line is all you asked it to return. you need to put it in a loop
    Code:
    while( Inputfile.getline(str4) )
    {
       sock.Send(num, str4);
    }
    Inputfile.close();
    since this is a c++ program you might as well use c++ class std::string
    Code:
    std::string str4;
    while( getline(Inputfile,str4) )
    {
       sock.Send(num, str4.c_str());
    }
    Inputfile.close();
    >>Im using ms vc++ 6.0
    And that is your second mistake. Toss that old compiler out and upgrade to free VC++ 2005 Express, especially if you are just learning the c++ language. you will learn some wrong stuff with your compiler.
    Last edited by Ancient Dragon; 04-10-2006 at 01:54 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> ifstream Inputfile(argv[0],ios::in);
    argv[0] is the executable, so unless you meant to send the contents of the executable file, you should probably use argv[1].

    >> delete str4;
    This should be delete [] str4 since str4 is a character array. Of course, you should generally prefer C++ strings as mentioned by Ancient Dragon, which clean themselves up automatically.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    13
    errors with string use


    Code:
     
    
    funcs.h(548) : error C2664: 'class std::basic_istream<char,struct std::char_traits<char> > &__thiscall std::basic_istream<char,struct std::char_traits<char> >::getline(char *,int)' 
    : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *'
            No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    funcs.h(560) : error C2664: 'Send' : cannot convert parameter 2 from 'const char *' to 'char *'
            Conversion loses qualifiers
    funcs.h(565) : error C2440: 'delete' : cannot convert from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to ''
            No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    funcs.h(565) : fatal error C1903: unable to recover from previous error(s); stopping compilation
    Error executing cl.exe.
    Mouse

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You have to do more than just chage it to string. You have to change the code to work with strings. It might seem like a hassle, but it is probably worth it in the long run.

    You have to use the string version of getline, you shouldn't be calling delete with a string (it cleans itself up automatically), and your sock.Send method won't work with the string class as is.

    If you can change the sock.Send method, you should have it take a string itself or at least a const char * since it probably doesn't need to actually modify the string. If you do that, then you can pass the string with c_str as shown.

  6. #6
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Don't forget to

    Code:
    #include <string>
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 11:51 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 09:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21