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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
02-25-2006, 06:48 AM
|
#1
|
Member
Registered: Apr 2005
Location: perugia
Distribution: ubuntu
Posts: 181
Rep:
|
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
|
|
|
02-25-2006, 09:22 PM
|
#2
|
Member
Registered: Oct 2003
Location: Tx,USA
Distribution: Slackware, Red Hat, CentOS
Posts: 495
Rep:
|
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;
}
|
|
|
02-25-2006, 11:21 PM
|
#3
|
Senior Member
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065
Rep:
|
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.
|
|
|
02-26-2006, 07:39 AM
|
#4
|
Member
Registered: Apr 2005
Location: perugia
Distribution: ubuntu
Posts: 181
Original Poster
Rep:
|
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
|
|
|
02-26-2006, 09:19 AM
|
#5
|
Member
Registered: Oct 2003
Location: Tx,USA
Distribution: Slackware, Red Hat, CentOS
Posts: 495
Rep:
|
#include <cstdio>
rename("old","new");
Not a big thing, you don't need the .c_str(), ... but you should specify the binary flag.
|
|
|
02-26-2006, 02:03 PM
|
#6
|
Member
Registered: Apr 2005
Location: perugia
Distribution: ubuntu
Posts: 181
Original Poster
Rep:
|
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
|
|
|
02-26-2006, 03:08 PM
|
#7
|
Member
Registered: Apr 2005
Location: perugia
Distribution: ubuntu
Posts: 181
Original Poster
Rep:
|
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");
}
}
|
|
|
All times are GMT -5. The time now is 12:57 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|