Thread: String Copying problems.

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    13

    String Copying problems.

    Ok, here is the deal. I need a method to copy characters. Sounds simple, right? well, I can't seem to be able to pull it off acurately.
    The problem is that my string I wish to copy has anywhere between 3 and 12 characters, and since this string is also a File name which i wish to load, accuracy is important.

    I can try this

    Char Filename[12];
    Char Lastname[12];

    cin.getline(Filename,12);
    strcpy(Lastname.Filename);

    and I get a "strcyp cannot convert 1 param from char to char *"

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    13
    And if someone could point me towards the syntax that allows you to copy the contents from file X to File y(overwriting file Y, but keeping the name) that would be great!

  3. #3
    Unregistered
    Guest
    First of all, with a full file name like "12345678.123" you will lose the last character. This is because getline(...) only reads n - 1 (in this case n == 12) characters to ensure there is space for the null terminator, which is how C/C++ knows where a string ends.

    Also, you should enter:
    strcpy(Lastname, Filename);

    Use commas to seperate parameters, not periods.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Re: String Copying problems.

    Originally posted by Red Army
    Code:
    Char Filename[12];
    Char Lastname[12];
    
    cin.getline(Filename,12);
    strcpy(Lastname.Filename); // it should be strcpy(Lastname,Filename);
    and I get a "strcyp cannot convert 1 param from char to char *" [/B]
    Check it..
    C++
    The best

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    317
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(){
            fstream file;
            file.open("myfile.txt",ios:: out|ios:: binary); //open file
            file.seekp(0, ios::beg); //move put marker to begginning
            file.write(const char*, int count); /* write something, I just put the prototype here */
            file.close; //close file
            return(0); 
    }
    This will open a file and overwrite the contents.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 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. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM