C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 23rd, 2004, 05:12 PM
chombee chombee is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 3 chombee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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?

Reply With Quote
  #2  
Old June 24th, 2004, 11:03 AM
chombee chombee is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 3 chombee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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();
}

Reply With Quote
  #3  
Old July 3rd, 2005, 10:24 PM
pgn674 pgn674 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Cumberland Center, Maine, USA
Posts: 1 pgn674 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 m 4 sec
Reputation Power: 0
Send a message via AIM to pgn674 Send a message via MSN to pgn674 Send a message via Yahoo to pgn674
Quote:
Originally Posted by chombee
rename(buffer,str);


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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > C++ file IO


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT