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, 04: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, 10: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, 09: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!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

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




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek