Thread: string misbehaving for ifstream

  1. #1
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80

    string misbehaving for ifstream

    Code:
    int MP4010::get_rows( string& file_name )
    {
    	int count;
    	int val;
    	ifstream inFile(file_name);
    	if(!inFile)	{
    		cerr<<"ERROR: The input file can not be opened."<<endl;
    		exit(1);
    	}
    	while (inFile) { 
    	    inFile.get(val);
    		count += val == '\n';
    	}
    
    	inFile.close();
    	return count;
    }
    Each time I try to compile I get the following 2 errors (lines 88 and 94 are highlighted in the code above):
    c:\...\mp_4010.cpp(88): error C2664: 'std::basic_ifstream<_Elem,_Traits>::basic_ifstrea m(const char *,std::_Iosb<_Dummy>:penmode)' : cannot convert parameter 1 from 'std::string' to 'const char *'
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>,
    _Dummy=int
    ]
    c:\...\mp_4010.cpp(94): error C2664: 'std::basic_istream<_Elem,_Traits>::_Myt &std::basic_istream<_Elem,_Traits>::get(_Elem &)' : cannot convert parameter 1 from 'int' to 'char &'
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]
    A reference that is not to 'const' cannot be bound to a non-lvalue
    I have no idea why this is happening. I am not converting between an int and a char, etc. Any ideas?

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    ifstream inFile(file_name.c_str());

    The function takes a char*, which <string>'s .c_str() member function returns.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80
    cheers for the lightning response! that solved the first problem (line 88 fixed) but the other error still appears.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    > cannot convert parameter 1 from 'std::string' to 'const char *'

    It means you are feeding it an std::string and it can't just convert that to a const char*, which is what it expects.

    The error happens here:
    ifstream inFile(file_name);

    And you only need to feed ifstream constructor with a char*. That can be easily achieved because strings have a member function called c_str() that returns a char* representation of the string.

    ifstream inFile(file_name.c_str());

    The second error I will leave to you to figure out. I think you can now interpret it. Give it a shot. And see what you can do to fix it.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80
    well i think if i replace int value; with char the 2nd error will be fixed. but i'm just wondering why i can't give it an int.

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Because the function doesn't take an int.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80
    right right, thanks! but the function gives strange results. when i input the following txt file:
    1 2 3 4 5 6 5 4 3 2 1
    6 5 4 3 2 1
    11 2 3 55 3 22 3 4
    the function should return 3, but it returns 2. now when i add a new line at the end (an empty 4th line basically) it returns 4. i don't understand why it's doing so.

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Because it expects an int

    But also because C++ will not automagically convert an int to char. You have to tell it to do it. C++ wouldn't know what to do, the poor fella. What would you want? The screen repesentation of the int? Or use it as an ascii value of the character to display?

    http://www.cppreference.com/cppio/get.html

    If then you need to use that char as an int (do ome calculations or something) you can convert it to int with a function like atoi() or using stringstreams.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Is there a reason you don't use inFile >> val to read in the integers?

  10. #10
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80
    the function is supposed to return the # of lines within the txt file. I have tried using that but it didn't work (always returned 0), so I switched and now it's barely working (like I said, returns incorrect values).

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In that case see my reply in the other thread for what I think is the best solution.

    A more low-level (beginner) solution is to get one character at a time and process it that way. When you read in a character that is a digit, then you can convert it to a number by subtracting the character '0' from it. So if you have a character ch and isdigit(ch) is true, you can convert it to an int with ch - '0'. Since your numbers can be multiple digits, you then have to combine them until you read in a non-digit. Combine them by multiplying the previous value by 10 and then adding the current digit.

    Using getline and stringstreams are easier and less error prone, but if you aren't allowed to use them you can try that method.

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    A more low-level (beginner) solution
    I don't seem to recall that "low-level" == "beginner"
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  13. #13
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    I got a php error when posting on this thread which caused duplicate entries, administrator.php for got the line number something about ';', i'd go look again but i might cause 10 more replies to show.

  14. #14
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    I don't seem to recall that "low-level" == "beginner"
    only 'n00bs' do ASM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM