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;
}