LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-25-2006, 05:48 AM   #1
rino.caldelli
Member
 
Registered: Apr 2005
Location: perugia
Distribution: ubuntu
Posts: 181

Rep: Reputation: 31
filename change in c++


I want to do a piece os software which encrypts and decrypts filenames... and I ddn't find anyhting simple and basic in linux...
so I started with a simple c++++ program using ftream etc etc..

however I didn't find a function to edit the filename of a file.. I tried therefore to copy a video content to another with this program but the program hangs??? why???

Code:
 
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
string filename;
cout << "Enter file name: ";
cin >> filename;
ifstream input(filename.c_str());
ofstream outfile ("standard.avi");
while(!input.eof())
outfile << input; 
}
thanks
 
Old 02-25-2006, 08:22 PM   #2
-X-
Member
 
Registered: Oct 2003
Location: Tx,USA
Distribution: Slackware, Red Hat, CentOS
Posts: 495

Rep: Reputation: 30
Probably want to read an encrypted file as binary, and the way it's read.
Read the entire file as something like;

Code:
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

int main(){
    ifstream in;
    ofstream out;
    long len;
    char *buff;
    
    in.open("in.bin", ios::binary);
    // get length
    in.seekg(0,ios::end);
    len = in.tellg();
    in.seekg(0,ios::beg);
    // make buffer
    buff = new char[len];
    out.open("out.bin", ios::binary);
    in.read(buff,len);
    out.write(buff,len);
    delete []buff;
    in.close();
    out.close();
    return 0;
}
or change your while loop to something like;

Code:
while (!in.eof()) {
    in.get(c);
    out << c;
}
 
Old 02-25-2006, 10:21 PM   #3
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
operator >> discards the end of line character.. which in a binary file such as avi, is actual data.. so do not use it even in binary mode..

do as -X- suggested and open in binary mode and then you can use read and write inside your while loop.. read and write are the standard way to handle binary files..

you need to read up on fstream and see how file streams are handled.. that should make things clearer.
 
Old 02-26-2006, 06:39 AM   #4
rino.caldelli
Member
 
Registered: Apr 2005
Location: perugia
Distribution: ubuntu
Posts: 181

Original Poster
Rep: Reputation: 31
thanks it works!!! this is my final code for those wo will read the post
Code:
 
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
string filename;
cout << "Enter file name: ";
cin >> filename;
ifstream input(filename.c_str());
char c;
ofstream outfile ("standard.avi");
while (!input.eof()) 
 {
    input.get(c);
    outfile << c;
 } 
}
Now do you know a function which handles filenames? to change them I mean? I searched through ifstream ofstream etc and didn't find any.. any hint? thanks a lot
 
Old 02-26-2006, 08:19 AM   #5
-X-
Member
 
Registered: Oct 2003
Location: Tx,USA
Distribution: Slackware, Red Hat, CentOS
Posts: 495

Rep: Reputation: 30
#include <cstdio>
rename("old","new");

Not a big thing, you don't need the .c_str(), ... but you should specify the binary flag.
 
Old 02-26-2006, 01:03 PM   #6
rino.caldelli
Member
 
Registered: Apr 2005
Location: perugia
Distribution: ubuntu
Posts: 181

Original Poster
Rep: Reputation: 31
thank you so much..
I figured it out and now it works... could you help me for the last part of the program?
How do I encrypt a string (the filename)??? is there a standard c++ library which does it???
because I couldn't find it.. thanks so much

Now I watch the olimpic games :-P
 
Old 02-26-2006, 02:08 PM   #7
rino.caldelli
Member
 
Registered: Apr 2005
Location: perugia
Distribution: ubuntu
Posts: 181

Original Poster
Rep: Reputation: 31
Code:
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
 int choice;
 cout<<"//////////////////////////////"<<endl;
 cout<<"1 filename change\n2 rename\n3 filename encryption"<<endl;
 cout<<"CTRL+C to exit"<<endl;
 cout<<"//////////////////////////////"<<endl;
 cin>>choice;
 if (choice==1)
 {
  string infilename;
  string outfilename;
  cout <<"Enter input filename: ";
  cin >>infilename;
  ifstream input(infilename.c_str());
  cout <<"Enter output filename: ";
  cin >>outfilename;
  ofstream output(outfilename.c_str());
  char c;
  while (!input.eof()) 
   {
    input.get(c);
    output << c;
   }
 }
 if (choice==2)
 {
  string infilename;
  cout <<"Enter input filename: ";
  cin >>infilename;
  ifstream input(infilename.c_str());
  rename(infilename.c_str(),"nuovofile");
 }
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
change uploaded files from filename.avi to filename.avi.html like www.rapidshare.de latheesan Linux - Newbie 3 06-16-2005 04:33 AM
How to change a mass of filename in a dir? superztnt Linux - General 4 07-27-2004 07:29 AM
Bash script to change a filename associated with an inode index number. Ziv Programming 22 06-19-2004 08:41 AM
Script to change several filename jan_81 Linux - Newbie 5 04-28-2004 11:10 AM
Can't change filename with SMB share InEeDhElPlInUx Linux - Security 0 04-01-2004 07:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration