| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
C++ file IO
Okay, Im writing a program to rename all my mp3 files cause my discman needs them in a certain format - with prefix 3 digit numbers.
So far, I have used the line: system("dir -1 *.mp3 >file_renamer.dat"); To create a file "file_renamer.dat" which lists all the mp3 files in the current directory, one per line. I then read each line into an array "buffer" and call my rename_file function for each, incrementing the count parameter each time: ifstream datfile("file_renamer.dat",ios::in); char buffer[100]; while (!datfile.eof()){ datfile.getline (buffer,100); rename_file(buffer,count); count++; } My rename_file function takes two parameters: "char buffer[]" which holds the name of the file to be changed and "int count" the number to be expanded to 3 digits and prefixed to make the new filename. In this function I create a new string "char str[100]" and put the desired new filename into it, then I do: rename(buffer,str); to rename the file. None of my files get renamed, with the perror function I get the reason "No such file or directory" but if I get my code to print out, via "cout<<buffer" the names of the files they are correct. So I'm thinking there is some unseen character(s) in my array "input" which cout removes but rename does not remove. Perhaps this is the null character appended to the end of the line by the getline function when reading the filename from file, or perhaps its due to the additional space because the variable "buffer" used for reading in filenames is of size 100 and longer than the filenames. Either way I can't figure out what to do. I learned to program in Java and I always find dealing with IO in C a real pain. Any help? |
|
#2
|
|||
|
|||
|
I thought it might be clearer if I posted the code as a whole also, it's quite short:
#include <iostream> #include <fstream> #include <stdio.h> #include<string> using namespace std; int rename_file(char input[],int count){ char str[100]; char newstr[100]; int i = sprintf(str, "%d ", count); if (i<3){ if(i==1) strcpy(newstr,"00"); if(i==2) strcpy(newstr, "0"); strcat(newstr,str); } strcat(newstr,input); if(!rename(input,newstr)){ cout<<"Renamed file: "<<input<<" to "<<newstr<<endl; }else{ cout<<"Failed to rename file: "<<input<<endl; perror("Reason: "); } } int main(){ int count = 0;/*counts the number of files renamed so far, which effects the new filenames*/ /*Have the system put a list of all mp3 files in the current directory into a file called file_renamer.dat, formatting one entry per line:*/ system("dir -1 *.mp3 >file_renamer.dat"); /*Go through the dat file one line at a time, renaming each mp3 file:*/ ifstream datfile("file_renamer.dat",ios::in); char buffer[100]; while (!datfile.eof()){ datfile.getline (buffer,100); rename_file(buffer,count); count++; } datfile.close(); } |
|
#3
|
|||
|
|||
|
Quote:
I beleive this will only take in constant character arrays, no variable ones (like buffer and str). I'm trying to find a way arround it as well. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > C++ file IO |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|