Thread: Can't see what's wrong with this

  1. #1
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448

    Can't see what's wrong with this

    Hi all,
    I get this error message:
    Error E2034 C:\Documents and Settings\alcampo\Escritorio\carlos\nomusic\main.cp p 53: Cannot convert 'char *' to 'char[]' in function viewbio(char *).
    This is the function:
    Code:
    void viewbio(char filename[])
    {
      char name[] = strcat(filename, ".txt"); //the error is here
      
      ifstream bio(name, ios::in); //open bio
      system("cls"); //clear the screen
      if(bio.is_open()) //if itīs open
      {
        while(!bio.eof()) //while itīs not the end of the file
        {
    	char c;
    	bio.get(c); //read a character...
    	cout<<c; //and print it to the screen.
        }
        bio.close();
      }
      else cout<<"Can't open file "<<name<<". Press any key to continue...";
      getch(); 
    }
    I've never had any problems with this function untill I tried to compile it this morning.
    TIA
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    What you need to do is..

    Code:
    char *name = strlen(filename + 5);
    strcpy(name, filename);
    strcat(name,".txt");
    
    // ...
    
    delete[] name;
    OR, if you decided to use string objects...

    Code:
    string name(filename);
    name += ".txt";

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    Originally posted by Eibro
    What you need to do is..

    Code:
    char *name = strlen(filename + 5);
    strcpy(name, filename);
    strcat(name,".txt");
    
    // ...
    
    delete[] name;
    Eibro must have typoed, I think he meant

    Code:
    char *name = new char[strlen(filename) + 5];
    strcpy(name, filename);
    strcat(name,".txt");
    
    // ...
    
    delete[] name;
    -Futura
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  4. #4
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    >>Eibro must have typoed.
    Yeah, seemed a bit strange to delete something when you don't create it. Tested like you said. Works now.
    Thanks a lot.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Codulation
    Eibro must have typoed, I think he meant

    Code:
    char *name = new char[strlen(filename) + 5];
    strcpy(name, filename);
    strcat(name,".txt");
    
    // ...
    
    delete[] name;
    -Futura
    Oops
    Yes, that's what I meant.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    What is the aversion to std::string on these boards?

    Code:
    #include<string>
    
    void viewbio(const std::string &name) 
    {
        std::string qualified(name);
        if(qualified.find('.') == std::string::npos ) {
            qualified += ".txt";  // appended only if no extention present
        }
        ifstream bio(qualified.c_str(), ios::in); //open bio
    
    // ...
    you can still call viewbio("mainbio"); thanks to the magic of binding temporaries to const references.

  7. #7
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    >>What is the aversion to std::string on these boards?
    The book I first studied C++ from I found looking thorough some computer-related stuff at home. It was really old and didn't explain the std::string at all so it's not my fault I don't know how to use them.
    You will be pleased to know I will study them shortly.
    Bye
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    One of the ongoing debates is whether to start with the "simple, slick, user friendly stuff" available in STL and learn what's under the hood later, if you want to; OR start from the basics and build up. You'll find zealots on both sides of the debate, as usual.

  9. #9
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    OK, now I have another problem. I want to acces the filename via an array of structs. For some reason when I read from the file, it wont copy the filename to the string.
    Code:
    struct person{char filename[30];};
    person *array = new person[NumberOfPeople()];
    //...
    ifstream people ("addresses.txt", ios::in); //open file
    //...
    people.getline(buffer ,30,'\n'); //Get the filename
         strcpy(array[i].filename, buffer);
         i++;
    When I try to access the filename. It's empty! Any Ideas? I'm out.
    TIA
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM