Thread: Reading text files

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    21

    Reading text files

    Hi, Im sorta new to c++, so i might need things explaining but what is the best way of reading a txt file, one character at a time and writing each character to a different file??

  2. #2
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    Programming is a form of art.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    21
    Ok, but how would I do that in a DLL. It comes up saying 'ofstream' undeclared

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    An ofstream undeclared error probably has nothing to do with it being a DLL. Did you #include <fstream>? Did you specify the std namespace?

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    Quote Originally Posted by Xinco
    Ok, but how would I do that in a DLL. It comes up saying 'ofstream' undeclared
    it sounds like you havn't included the <fstream> header file

    does it say this at the top of your code >

    #include <fstream>

    ^^ this header is needed for FILE IO ^^
    WhAtHA hell Is GoInG ON

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Just look at those tutorials.
    Code:
    #include <fstream>
    using std::ios;
    int main()
    {
       std::fstream dll_rw("my_libs.dll",ios::binary | ios::in | ios::out);
       if(!dll_rw) 
       {
          //deal with open failure
       }
       else  //"else" usually not necessary
       {
          //....read and write to DLL file....
          dll_rw.close();   //finished -- not necessary, but tidy
       }
       return 0;
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    21
    Ok, i found the answer but now its saying 'invalid conversion from 'char' to 'const char''. I thinks its because im using ifstream read_file(argumen0); but I need argument0 to be an argument so how do i fix this

  8. #8
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    You seem to be reading a character at a time, rather than line at a time. A const char* is a c style string. You should in the else statement, read each line, preferrably into a string (#include <string>), then go on to the next. You should all do this while the file has a next line.

    Edit, I see you want to read it a character at a time, since a string can be treated as an array of char, and each char can be referenced with the [] operator, you can read the string, then do a for loop that goes through each char of the string and outputs them.

    Code:
        std::string s = "akdnknfknef";
    
        for(std::string::size_type i = 0; i < s.size(); ++i)
            std::cout << s[i] << std::endl;
    replace the cout with the output file stream
    Last edited by indigo0086; 01-08-2007 at 10:54 AM.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    21
    But I need it to be written 1 character at a time so I can change the ascii value. Sort of like how encryption works (I think) Unless it is possible to change the ascii value of a whole line by a set amount per character.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you are getting errors it is generally better to post the exact error message and your code so you can get specific help.

    As far as reading a character at a time, I would use get() for input, then encrypt the character, then output it. That will move things along one character at a time.

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    My method;

    Code:
    int main()
    {
    ifstream fin ("your.dll", ios::in | ios:: beg |ios::binary);
    ofstream out("new.dll, ios::out | ios::binary |ios::app);
    
     // verify file open success
    
     while(!fin.eof())  
         { 
      
    
       char input = 0;
        fin.unsetf(ios::skipws); // turn off whitespace skipping
        fin >> input;
       
       //manipulate, encrypt input (1 char)
    
        out<<input;   //write encrypted byte to output file ("new.dll")
     
        } //end while loop
    
     //close files
    
     return 0;
    }
    I think I've got that right. If not, I'll hear about it!

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    while(!fin.eof())
    Might want to read this. http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Quote Originally Posted by Xinco
    But I need it to be written 1 character at a time so I can change the ascii value. Sort of like how encryption works (I think) Unless it is possible to change the ascii value of a whole line by a set amount per character.
    with my method you can write one char at a time, and manipulate it as such char by char.

  14. #14
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252

    Red face

    Quote Originally Posted by dwks
    Code:
    while(!fin.eof())
    Might want to read this. http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    Actually, I have. It just continually slips my mind.

  15. #15
    Registered User
    Join Date
    Nov 2006
    Posts
    21
    Quote Originally Posted by Oldman47
    My method;

    Code:
    int main()
    {
    ifstream fin ("your.dll", ios::in | ios:: beg |ios::binary);
    ofstream out("new.dll, ios::out | ios::binary |ios::app);
    
     // verify file open success
    
     while(!fin.eof())  
         { 
      
    
       char input = 0;
        fin.unsetf(ios::skipws); // turn off whitespace skipping
        fin >> input;
       
       //manipulate, encrypt input (1 char)
    
        out<<input;   //write encrypted byte to output file ("new.dll")
     
        } //end while loop
    
     //close files
    
     return 0;
    }
    I think I've got that right. If not, I'll hear about it!
    Thats an app, I need the code for a programming the DLL. Heres what I got:

    DLL.h: (Note, this is header)
    Code:
    #ifndef _DLL_H_
    #define _DLL_H_
    
    #define export extern "C" __declspec (dllexport)
    
    #if BUILDING_DLL
    # define DLLIMPORT __declspec (dllexport)
    #else 
    # define DLLIMPORT __declspec (dllimport)
    #endif 
    
    
    class DLLIMPORT DllClass
    {
      public:
        DllClass();
        virtual ~DllClass(void);
    
      private:
    
    };
    
    
    #endif
    DLLmain.cpp: (this is dll c++ code)
    Code:
    #include "dll.h"
    #include <windows.h>
    #include <fstream>
    
    using namespace std;
    
    export double func1(char argument0,char argument1)
    {
           ifstream readfile;  //Here I get the error "invalid conversion from 'char' to 'const char'"
           ofstream writefile;
           readfile.open (argument0);
           writefile.open (argument1);
           char ch;
           while (readfile.get(ch)) {
           writefile << ch;
           }
           readfile.close();
           writefile.close();
    }
    I also tried this for the dllmain.cpp
    Code:
    #include "dll.h"
    #include <windows.h>
    #include <fstream>
    
    using namespace std;
    
    export double func1(char argument0,char argument1)
    {
           ifstream read_file(argument0);  //Here I get the error "invalid conversion from 'char' to 'const char'"
           ofstream write_file(argument1);
           char ch;
           while (read_file.get(ch)) {
           write_file << ch;
           }
           read_file.close();
           write_file.close();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading from text file
    By jamez in forum C Programming
    Replies: 3
    Last Post: 11-30-2005, 07:13 PM
  2. reading text files
    By stimpyzu in forum C++ Programming
    Replies: 11
    Last Post: 04-17-2004, 07:45 AM
  3. Reading spaces, carrage returns, eof from text files
    By thenrkst in forum C++ Programming
    Replies: 1
    Last Post: 03-11-2003, 05:18 AM
  4. reading certain parts of text files
    By Captain Penguin in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2002, 09:45 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM