LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   filename change in c++ (https://www.linuxquestions.org/questions/programming-9/filename-change-in-c-419357/)

rino.caldelli 02-25-2006 05:48 AM

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

-X- 02-25-2006 08:22 PM

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;
}


xhi 02-25-2006 10:21 PM

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.

rino.caldelli 02-26-2006 06:39 AM

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

-X- 02-26-2006 08:19 AM

#include <cstdio>
rename("old","new");

Not a big thing, you don't need the .c_str(), ... but you should specify the binary flag.

rino.caldelli 02-26-2006 01:03 PM

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

rino.caldelli 02-26-2006 02:08 PM

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 01:37 AM.