Join 132,383 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,191 people online right now. Registration is fast and FREE... Join Now!
I'm having a real problem with mkdir(). I think I'm getting a segfault from it.
First off, I can't find it in any reference guide (well, no real info on the net, not even on cplusplus.com).
Second, what are the possible codes for for mkdir? Can't find them anywhere! I have no access to this directory I create either. Like, I click on it and I'm told I don't have the permissions to access it. I'm pretty sure it's something to do with the second parameter of mkdir. This is Linux by the way.
Do I have to "open" a folder in order to write to it? I'm trying to make this directory and just write files to it but now I think that one problem is that I haven't opened the directory.
PS: This is unrelated but how can I enter a number into a string and have it increment? I was having a problem with that.
LOL. That won't work on Unix. The Unix version requires a mode parameter. The Windows version doesn't. That's what the reference to the variadic function was about. open() is a variadic function, and it also has a mode parameter, which is required depending on the other parameters passed to open. The compiler doesn't catch this kind of thing, except when programmed specifically to do so (like with printf). It can cause some strange behaviors, such as creating files with strange permission sets (being unable to open the file after writing it).
This post has been edited by perfectly.insane: 31 Aug, 2008 - 07:30 AM
for(int k = 0; k < 3800;k++) { outFile << fpieces.at(k+(3799*i)); } outFile.close(); }
//cout << fpieces<<"\n"; //outFile << fpieces; } //Cuts excess from filename and extracts name to be used as a directory string directoryName(string file) { string retString = file; int position = 0;
/* void fileCutter(ifstream& input, string name) { int charCount = 0; int fNum = 1; string fstr; ofstream outFile; //Writes the files while(input.eof()==false) { //Sets string to null fstr=NULL; //Places 3850 characters in a string to be written to file //while 3850 hasn't been reached or the end of the file do { strcat(fstr, input.get()); charCount++; }while(charCount%3850!=0 && input.eof()==false); outFile.open(name+"part_"+fnum+".txt", ios::trunc); outfile << fstr; outFile.close(); fNum++; } } */
int main(int argc, char* argv[]) { //Checks to see if there is was an argument through commandline string fileName; string dirName; if(argc>1) fileName = (string)*(argv+1); long size = 0; ifstream inFile; int fileCount = 0; //Opens file depending on if there's input from terminal or not. try { if(argc>1) if(fileExists(fileName)==true) { //Outputs size of file and opens it inFile.open(fileName.c_str()); size = fileResults.st_size; cout << size<<endl; } else throw string("File does not exist.\n"); else { cout << "Enter the name of the file:\n"; //Accepts input for fileName but also allocates 1 more space //for a null character
cin >> fileName;
if(fileExists(fileName)==true) { //Outputs size of file and opens it //While throwing an exception if we can't open the file inFile.open(fileName.c_str()); if(inFile.bad() == true) throw string("Cannot open file."); size = fileResults.st_size; cout << size<<endl; } else throw string("File does not exist.\n"); } //If fileName really is a file but not a text file //throw an error. if(strcasestr(fileName.c_str(), ".txt")==NULL) throw string("Not a text file.\n"); dirName = "/home/tetrix/Documents/"+directoryName(fileName); cout<<dirName<<endl; //dirName = strcat(dirName, fileName.c_str()); //Make a directory with the name of the file being read mkdir(dirName.c_str(), 0777); cout << "Directory successfully created"<< endl; fileWriter(fileCutter(inFile),dirName.c_str()); //fileCutter(inFile,"/home/tetrix/Documents/"+fileName+"/"); cout << "File cut!"; }
No, you don't need to "open" the directory to write files. You only do that when you need to access the index directly (listing files).
I'm not really sure what you're trying to do here. If you want to read the entire file, there are some way easier ways of doing it.
ifstream ifs(...);
// Create some array the size of the file... char* buf = new char[filesize]; ifs.read(buf, filesize); // or ifs.rdbuf()->sgetn(buf, filesize); ifs.close();
As far as the segfault, I'm not sure what's causing that, but I do know that you should call outFile.clear() at the end of your loop in the fileWriter function if you want to keep using the same ofstream object multiple times.
Thanks man. I'll use that method for reading a file. I'm still new at this so it'll be a while before I can write cryptic yet efficient code.
Anyway, my objective is to read a file, and then write it to a file, creating a new file every 3.8kb, and stick all of those files into a folder. It's for the iPod, to get over it's 4kb limit.
For the directory modes, I read the thing and what I'm getting is that it's the same as file permission in Linux. Is that right?
By the way... have you tried the split utility (I think it already does what you're trying to do)?
So yeah, it's the octal for permissions in linux. However, when I try to go to it, I'm still told that I don't have permission for opening the folder. Do I have to have mode_t mode = 0777? I was just putting 0777 straight into the second parameter.
I haven't tried the split utility. I imagine it would be easier to use. What is it in, boost? How do I download and use other libraries besides the standard library.
Before I go on my merry way though, I really want to know why is being retarded and giving me a segfault and how to fix it. You know, for learning purposes.