Problem with simple file program

This is a discussion on Problem with simple file program within the C++ Programming forums, part of the General Programming Boards category; Here's a simple file program i have been testing to learn some file manipulation.. Code: #include <iostream> #include <string> #include ...

  1. #1
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193

    Problem with simple file program

    Here's a simple file program i have been testing to learn some file manipulation..

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <fstream>
    
    using namespace std;
    
    int main() {
    string filepath;
    ofstream outputfile ("output.txt", ios::out | ios::app | ios::binary);
    
    cout << "File Copier =P" << endl;
    cout << "Input file name: ";
    cin >> filepath;
    ifstream inputfile (filepath, ios::in | ios::binary);
    inputfile.open(filepath);
    if (inputfile.is_open()) {
    long begin, end, total;
    int parts;
    char * memblock;
    begin = inputfile.tellg();
    inputfile.seekg (0, ios::end);
    end = inputfile.tellg();
    total = end-begin;
    inputfile.seekg (0, ios::beg);
    total = total/1024;
    if(total != 0) {
    parts = total;
    }
    else {
    parts = 1;
    }
    for(int i=0;i<parts;i++) {
    inputfile.read (memblock, 1024);
    outputfile.write(memblock, 1024);
    delete[] memblock;
    }
    inputfile.close();
    outputfile.close();
    return 0;
    }
    
    }
    But im getting this error

    From spanish to english..
    "no se encontró una función coincidente para la llamada a" -> a function as the written there wasn't found
    " los candidatos son: " -> the candidates are

    Code:
    helloworld.cpp: In function ‘int main()’:
    helloworld.cpp:15: error: no se encontró una función coincidente para la llamada a ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string&, std::_Ios_Openmode)’
    /usr/include/c++/4.2/fstream:464: nota: los candidatos son: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
    /usr/include/c++/4.2/fstream:450: nota:                     std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with _CharT = char, _Traits = std::char_traits<char>]
    /usr/include/c++/4.2/iosfwd:89: nota:                     std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)
    helloworld.cpp:16: error: no se encontró una función coincidente para la llamada a ‘std::basic_ifstream<char, std::char_traits<char> >::open(std::string&)’
    /usr/include/c++/4.2/fstream:517: nota: los candidatos son: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
    Any help?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    ifstream inputfile (filepath.c_str(), ios::in | ios::binary);
    The file constructors take C-style strings, not std::strings.

  3. #3
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Well.. thanks but I'm still getting that error, I'm compiling using g++ filename.c -o filename

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's because you forgot to remove the line with the .open() on it (since that doesn't do anything, since the constructor opens the file). (The open would also require .c_str() on it too, if it was necessary, which it isn't.)

  5. #5
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Mmm.. im getting a segmentation failure, don't you know why?
    Last edited by lautarox; 09-07-2008 at 07:25 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You never allocate 1024 characters on the other end of memblock -- currently it points to somewhere north of Secaucus, New Jersey, and that's just not a writable memory address.

  7. #7
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Ohh thanks, I'm actually getting some errors on the output file.. like txt files can't be readen, things like that, is it because I'm creating the txt file and then writing the binary code to it? if I open it, I can see all the text, and some other text also, not originally written there.
    I'm also needing a good c++ socket tutorial (linux), because all i get is c/c++ tutorials and i think they are not good for c++.
    Last edited by lautarox; 09-07-2008 at 08:51 PM.

  8. #8
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    And I also want to know how to know if a file exists, thanks

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by lautarox View Post
    And I also want to know how to know if a file exists, thanks
    Try to open it for reading - if that works, the file exists. If it doesn't work, it is unfortunately not proof that it DOESN'T, but it is proof that the file can not be opened for reading, which is usually "close enough" if you are using it for "Are you sure you want to overwrite the existing file ...." type warning/question (in other words, if you can't open the file for reading, it either doesn't exist (what we wanted to know), you don't have the rights to open it for reading so you probably can't write to it either (so we will get an error when trying to create a file with that name), or some other process is holding the file open preventing our process from openining it - again, we'd have an error indicating that if this when trying to create the new file).



    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    > I'm also needing a good c++ socket tutorial (linux), because all i get is c/c++ tutorials and i think they are not good for c++.

    Did you check the stickies at the top of the networking forum? Also, why would a C/C++ API not be good for C++?

  11. #11
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Well.. there's still the problem on copying the file, the output file can't be readen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. problem in file merger program
    By san_crazy in forum C Programming
    Replies: 6
    Last Post: 07-09-2008, 08:52 AM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

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