Thread: char[] manipulation

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    12

    Unhappy char[] manipulation

    I'm writting a multiple file handling program and need to build filename and path strings.

    I want the user to be able to enter a path and then append the filename to the end of this. However when using strcat, the null termination character seems to be ignored

    Can anyone show me what I am doing wrong and explain why.

    Attached is a file listing and a jpeg of the output.

    Many thanks for any help given.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <stdio.h>
    
    using namespace std;
    
    int main ()
    {
    int length;
    string line;
    char filename1[8] = "net.txt", out_f[8] = "out.txt", file1[80],file2[80],path[80];
    
    cout << "Please Enter full path of first input file \n";
    cout << "eg : C:dir1\\dir2\\ \n";							//double \\ to display single \
    cin.get(path,80);											//get file path
    
    length = strlen(path);										//find path length					
    cout << "\n" << length << "\n";
    strncpy(file2,path,length);									//build full file name + path
    strncat(file2,out_f,8);
    //strcat(file2,"\0");
    strncpy(file1,path,length);
    strncat(file1,filename1,8);
    //strcat(file1,"\0");
    
    cout << "file paths built";				
    
    cout << "\n";												//show whats going on
    cout << "path " << path;
    cout << "\n";
    cout << "input " << file1 << "\n";
    cout << "output " << file2 << "\n";
    
    ifstream file_1 (file1);									//open file
    	if (file_1)
    	{														//succeeded
    		ofstream file_2(file2,ios::out);
    		while (!file_1.eof())								
    		{													//read file
    			getline (file_1,line);
    			cout << line << endl;
    			file_2 << line << endl;							//save in new file
    		}
    		file_1.close();
    		file_2.close();
    	}
    	else cout << "error";
    return 0;
    }
    Last edited by Salem; 06-21-2007 at 02:54 AM. Reason: Added [code][/code] tags - learn how to use them, see the intro thread.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Please place your code in [code][/code] bbcode tags. At a glance, why are you not using C++ std::string when you have included the appropriate header?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    12

    Smile

    I now have the code working, I didn't realise I had to load the char[] with a null before appending to it.

    I'd like to use string, but the file handling commands (ifstream/ofstream) don't except them as valid parameters. I got compiler errors when I tried it.

    Thanks anyway

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'd like to use string, but the file handling commands (ifstream/ofstream) don't except them as valid parameters. I got compiler errors when I tried it.
    There is the c_str() member function just for those times, e.g.,
    Code:
    std::string filename;
    std::cin >> filename;
    std::ifstream in(filename.c_str());
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    thanks i'll give it a go

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question regarding char *argv[]
    By cnb in forum C Programming
    Replies: 6
    Last Post: 10-08-2008, 04:50 AM
  2. Char[] of C# to what in C++
    By Arkanos in forum C++ Programming
    Replies: 3
    Last Post: 02-13-2006, 08:10 AM
  3. Bit Manipulation Questions
    By CPPNewbie in forum C++ Programming
    Replies: 7
    Last Post: 08-12-2003, 02:17 PM
  4. Replies: 18
    Last Post: 06-21-2003, 10:57 AM
  5. File manipulation
    By Shadow in forum C Programming
    Replies: 1
    Last Post: 04-23-2002, 08:07 AM