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??
Printable View
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??
Ok, but how would I do that in a DLL. It comes up saying 'ofstream' undeclared
An ofstream undeclared error probably has nothing to do with it being a DLL. Did you #include <fstream>? Did you specify the std namespace?
it sounds like you havn't included the <fstream> header fileQuote:
Originally Posted by Xinco
does it say this at the top of your code >
#include <fstream>
^^ this header is needed for FILE IO ^^
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;
}
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
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.
replace the cout with the output file streamCode:std::string s = "akdnknfknef";
for(std::string::size_type i = 0; i < s.size(); ++i)
std::cout << s[i] << std::endl;
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.
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.
My method;
I think I've got that right. If not, I'll hear about it!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;
}
Might want to read this. http://faq.cprogramming.com/cgi-bin/...&id=1043284351Code:while(!fin.eof())
with my method you can write one char at a time, and manipulate it as such char by char.Quote:
Originally Posted by Xinco
Quote:
Originally Posted by dwks
Actually, I have. It just continually slips my mind.
Thats an app, I need the code for a programming the DLL. Heres what I got:Quote:
Originally Posted by Oldman47
DLL.h: (Note, this is header)
DLLmain.cpp: (this is dll c++ code)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
I also tried this for the dllmain.cppCode:#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();
}
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();
}