Thread: strcopy problems, file I/O

  1. #1
    Registered User
    Join Date
    May 2012

    strcopy problems, file I/O

    I'm trying to let the user dynamically enter the name for text files that they are reading into my program, as well as what they what the output to be.

    I know with C++, that ifstream and ofstream are what's needed and those are working fine.

    I do a cin>>input and read it into a char array(cstring). I have included string.h with my includes. Now the .open function requires a cstring as an input. Something like

    cout<<"put the name of the file you want to open: ";
    cin>>inputfile; (inputfile);
    As is, this works ok but you have to specify the .txt part of the file and I would like to take it a bit further.

    I want to automate the .txt part, as well as use the input filename as part of the 2 output filenames that I want.

    Basically, I want to read in from say: Test1.txt.

    For my program I want to only have the user input "Test1", and have my program add the .txt to it before it does the part.

    I have tried doing:

    strcopy(inputfile2, inputfile);
    I put the inputfile into another container because I want to reuse inputfile for the 2 outputs like so:

    strcat(outputfile," -translation.txt");
    strcopy(reportfile, inputfile);
    strcat(reportfile," -reportfile.txt");;;
    Which should give me "Test1.txt" as my input, and "Test1 -translation.txt", and "Test1 -reportfile.txt" as the 3 different filenames. But it's not working right.

    Remember, I have included string.h, and I have also tried it with <cstring>. I'm not really sure what else I can do, is my compiler so old(its not a very up to date linux box) that it doesn't have this function built in?

    Every time I compile I get "'strcopy' was not declared in this scope"

    The only reason I'm doing it with C strings is because requires it's input to be a C string, otherwise I would use the string container. I could also use the c_str() to convert a C++ string to C, but then I would still run into the same issues using strcopy.

    Any help would be appreciated.

  2. #2
    Registered User
    Join Date
    Oct 2006
    why are you using char arrays instead of std::string?

  3. #3
    Registered User
    Join Date
    May 2012
    I was using char arrays because it seemed logical. The .open function call will not take std::string as an argument, only char arrays. So it seemed logical to use those rather than having to convert on the fly.

    But I think I realize what I did wrong, which is mispell strcpy. I put strcopy which obviously isn't correct. And once I fixed that everything worked perfectly.

    I can't find where to delete the thread, so if a mod wants to, that's ok with me. Sorry if I wasted someone's time, but maybe it will help someone else with a similar problem lol.
    Last edited by oopsyourhead; 12-06-2012 at 04:13 PM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    if you have a compiler that supports the latest standard (C++11), std::ifstream and its friends have overloads of the constructors and open members that take a std::string. in any case, it's not difficult to say .c_str() when calling open, and using std::string will save you lots of headaches down the road. you will never overrun your buffers, leading to safer code. also, using operator >> will fail to work correctly if your filename contains whitespace. for that you'll need to use cin.getline(buffer, length) or std::getline(cin, some_std_string). it's a good idea to get in the practice of using std::string for all of your string handling needs. concatenation is done with the + operator, individual character access is done with square brackets [], and comparison is done with == and !=. it doesn't get much simpler. using std::string, you'll find that you are much more productive, because you need to worry about fewer things. a good exercise for a beginning C++ programmer might be to write your own string class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems Outputting File Data To Text File
    By noname88 in forum C Programming
    Replies: 1
    Last Post: 10-04-2011, 01:12 PM
  2. Replies: 9
    Last Post: 05-25-2011, 02:59 AM
  3. strcopy for an array
    By the Wookie in forum C++ Programming
    Replies: 14
    Last Post: 11-04-2003, 09:26 AM
  4. Problems with file i/o
    By Dual-Catfish in forum C++ Programming
    Replies: 2
    Last Post: 07-03-2002, 07:58 AM
  5. How to strcopy a single character from a string
    By Stuu in forum C++ Programming
    Replies: 8
    Last Post: 04-18-2002, 01:45 AM