char function

This is a discussion on char function within the C++ Programming forums, part of the General Programming Boards category; hey, This should be pretty simple: I need to make a function which returns a person's name from a file ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    29

    char function

    hey,
    This should be pretty simple:

    I need to make a function which returns a person's name from a file

    Code:
    #include <fstream>
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    char loadName(char def[100])
    {
    char r[20];
    ifstream file(def);
    file.getline(r, 20);
    return r[0];
    }
    
    
    int main()
    {
    char name[20];
    char def[100] = "c:\\test.txt";
    
    cout << loadName(def);
    cin.get();
    }
    What ends up happening is, I have to return the first letter of the person's name, I can't just go
    return r;

    How would I be able to return a string as opposed to just one char

    Thanks in advanced

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Well of course since you are using C++, you could be using STL strings - which make life a lot easier. But obviously you're learning with C-style strings, so we'll keep using them. When you're more comfortable with C++, you might want to have a look at STL strings.

    The best way to return a string is to return a pointer to the first element of the array.

    Code:
    char *pointer = &r[0];
    return pointer;
    If you haven't learned about pointers yet, now might be a good time to do so - try the tutorial on this site. They may not make sense at first, but stick with it - they're useful, no matter how pointless (no pun intended) they seem at first.

    When your other function gets this pointer it can use it to get the string. You can also just pass cout the pointer and it'll print the string to the screen.

    I can't just go
    return r;
    Actually, you can and should - that code would actually return a pointer to the first element of the array! There's your little-known C++ fact of the day.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    You might be able to try strncpy(), but I'm not very familiar with the C functions so I cant say for sure.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    29
    ok,
    I learned pointers.
    I still have the same problem though.
    I can only return one array
    so I used this code:

    Code:
    #include <fstream>
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    char loadName(char def[100])
    {
    char r[20];
    ifstream file(def);
    file.getline(r, 20);
    char *pointer = &r[0];
    return *pointer;
    }
    
    int main()
    {
    
    def[100] = "c:\\test.txt";
    
    
    cout << loadName(def);
    cin.get();
    }
    as you can see this only returns first letter. I need to return the entire string. Thanks

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's one way.
    Code:
    #include <fstream>
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    char loadName(char def[100], char name[])
    {
    ifstream file(def);
    if (!file.is_open())
    {
       cout << "Unable to open file: " << def << endl;
       exit(1);
    }
    file.getline(name, 30);
    }
    
    int main()
    {
    
    char def[100] = "c:\\test.txt";
    char name[30];
    
    loadName(def, name);
    cout << name << endl;
    cin.get();
    }
    Last edited by swoopy; 07-02-2005 at 11:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM

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