#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    24
    Rep Power
    0

    read file problem


    hello all:
    Would anyone can help me let my code can read any type of file, like .pdf, ppt, html,
    image file like, .gif, .jpeg, etc.
    my code just can read .txt file.

    rgds
    stewang
    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    void main(){
      ifstream myin;
      ofstream myout;
      char readfilename[20], writefilename[20];
      char word[20];
      
      cout << "please the filename which you want to read-> ";
      cin >> readfilename;
    
      myin.open(readfilename);
      if(!myin){
        cout << "Can't open file";
        exit(1);
      }
    
      cout << "please the filename which you want to read-> ";
      cin >> writefilename;
      myout.open(writefilename);
      if(!myout){
        cout << "Can't open file->" << writefilename << endl;
        exit(1);
      }
      
      while(myin>>word){
        myout << writefilename;
    
      }
    }
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    Try opening your files in binary mode like this:
    Code:
    myin.open(readfilename, ios::binary);
    myout.open(writefilename, ios::binary);
    The default is to open them in text mode rather than binary mode (on OS's that care about file modes, that is), so you have to explicitly specify the mode.
    Last edited by Scorpions4ever; September 2nd, 2003 at 05:34 PM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    24
    Rep Power
    0
    Thanks for your respones.
    But I got some problems when I run my code in Linux with different file.

    1> when I read a .txt which contains strings of "I an test now" from try1.txt, then I wrote it try8.txt.
    and I found some sentences in try8.txt as following
    "try8.txttry8.txttry8.txttry8.txt"
    it should be the string "i an test now" in try8.txt

    2> when I tested it with different files, the complains as
    following;

    please the filename which you want to read-> pic.jpg
    please the filename which you want to read-> pic2.jpg
    Segmentation fault

    please the filename which you want to read-> talk.pdf
    please the filename which you want to read-> talk2.pdf
    Segmentation fault
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    I'll answer your two questions separately, although the error is in the same spot.

    Question 1:
    The error is here
    Code:
    while(myin>>word){
        myout << writefilename; // <---- Here's the error!
    }
    You are writing out the variable "writefilename", instead of the data that you're reading in to the variable "word" :D. This is why you get the output as "try8.txttry8.txttry8.txttry8.txt".

    Question 2:
    If you're using a char array, it is never a good idea to use myin>>word or myout << word, especially since you have no idea how long the string to be read is. You're looking at a serious buffer overflow problem there. Instead, you should be using the stream read() and write() methods.

    Other problems include:
    (a) Not closing your streams after you're done with them
    (b) void main() is a bad idea (Illegal in C++ actually).

    With that said, your program could be rewritten something like this:
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(void){
      ifstream myin;
      ofstream myout;
      streamsize readin;
      char readfilename[20], writefilename[20];
      char word[20];
    
      cout << "please the filename which you want to read-> ";
      cin.getline(readfilename, 20);
    
      myin.open(readfilename, ios::binary);
      if(!myin){
        cout << "Can't open file";
        return(1);
      }
    
      cout << "please the filename which you want to write-> ";
      cin.getline(writefilename, 20);
      myout.open(writefilename, ios::binary);
      if(!myout){
        cout << "Can't open file->" << writefilename << endl;
        return(1);
      }
    
      while(myin.read(word, 20)){
        readin = myin.gcount();
        myout.write(word, readin);
      }
      readin = myin.gcount();
      if (readin)
        myout.write(word, readin);
    
      myin.close();
      myout.close();
    
      return 0;
    }
    Incidentally, I'd originally assumed that you were using Windoze or DOS, not Linux. It just happens that Linux doesn't care if you open a file in binary or text mode, so it doesn't really matter if you do:
    myin.open(readfilename, ios::binary);
    or
    myin.open(readfilename);
    It is only on DOS or Windoze, where you have such a problem. Also note the use of cin.getline(readfilename, 20) instead of cin >> readfilename --- it's safer (no risk of buffer overflow).
    Last edited by Scorpions4ever; September 2nd, 2003 at 06:59 PM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    24
    Rep Power
    0
    Thanks Scorpions4ever, it works.
    And I added your idea to my code, it comes out different
    result compare with the original data.
    I sent my code to your private email box already, please help me.

    rgds
    stewang

IMN logo majestic logo threadwatch logo seochat tools logo