| View previous topic :: View next topic |
| Author |
Message |
123qwe Newbie cheater
Reputation: 0
Joined: 26 Dec 2006 Posts: 21
|
Posted: Mon Jan 19, 2009 12:05 am Post subject: C++ File Output |
|
|
how would i go about loading "password.txt" into a char* one letter/number at a time?
i have this code, but it reads all of the file(file is only one line around 32 characters). i thought about using hte get function but char* doesnt work with it(from what i tried)
| Code: | int nsize = 32;
char* passwds = new char[32];
ifstream myfilea;
myfilea.open("password.txt");
myfilea.read(passwds,nsize);
myfilea.close(); |
thanks for any help =]
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Mon Jan 19, 2009 12:33 am Post subject: |
|
|
http://cplusplus.com/doc/tutorial/files.html
Look at the seekg() and seekp() section
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Mon Jan 19, 2009 1:15 am Post subject: |
|
|
Create a loop and just read one char at a time. It would be better to just find out the size and only use read once.
| Code: |
char * passwds = new char[32];
for (int i = 0; i < 32; i++)
passwds[i] = 0;
std::ifstream iFile("password.txt", std::ios::in);
char * ptrPasswds = passwds;
while(!iFile.eof())
iFile.read(ptrPasswds++, 1);
iFile.close();
|
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Mon Jan 19, 2009 7:36 am Post subject: |
|
|
Why to read one char at a time? It's not very wise.. Why not to read the whole block and then do your stuff one char at a time when it's loaded into mem?
File I/O is _slow_, so it's always better to read a little larger block of data than one byte.
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Mon Jan 19, 2009 8:28 am Post subject: |
|
|
dont forget to delete what you call new on
| killersamurai wrote: | Create a loop and just read one char at a time. It would be better to just find out the size and only use read once.
| Code: |
char * passwds = new char[32];
for (int i = 0; i < 32; i++)
passwds[i] = 0;
std::ifstream iFile("password.txt", std::ios::in);
char * ptrPasswds = passwds;
while(!iFile.eof())
iFile.read(ptrPasswds++, 1);
iFile.close();
|
|
_________________
|
|
| Back to top |
|
 |
123qwe Newbie cheater
Reputation: 0
Joined: 26 Dec 2006 Posts: 21
|
Posted: Mon Jan 19, 2009 4:43 pm Post subject: |
|
|
i found out c++ has a substr function, unfortuantly it works only for strings. how would i make it work with char* ?
my attempt is below, Note that i want it to read one char at a time, and the TypeStr function only accepts char*
| Code: | int nsize = 32;
char* passwds = new char[32];
ifstream myfilea;
myfilea.open("password.txt");
myfilea.read(passwds,nsize);
myfilea.close();
char* cha = new char[1];
int i;
for(i = 0; i <= strlen(passwds); i++)
{
cha = passwds.substr(i,i+1);
TypeStr(cha);
Sleep(1000);
} |
and these are my compile errors
| Code: | .\main.cpp(93) : warning C4018: '<=' : signed/unsigned mismatch
.\main.cpp(95) : error C2228: left of '.substr' must have class/struct/union
type is 'char *' |
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Mon Jan 19, 2009 5:45 pm Post subject: |
|
|
Well the warning is telling you exactly whats wrong. Your comparing a signed integer to a unsigned integer. Because you initialized i as an int, while strlen returns an unsigned int.
Second is because char * isn't a struct/class/union. You are confusing yourself with the type string, which is a class that deals with characters for you. It has inline functions like that. Anyways your better off just doing something like this:
| Code: | BOOL GetPassword(__in_z LPTSTR lpFileName, __out_z LPTSTR lpPassword)
{
HANDLE hFile;
DWORD dwSize, dwBytesRead;
BOOL bRET = FALSE;
if (lpPassword != NULL)
{
hFile = CreateFile(lpFileName, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile != INVALID_HANDLE_VALUE)
{
dwSize = GetFileSize(hFile, 0);
if (dwSize != INVALID_FILE_SIZE)
{
if (ReadFile(hFile, (LPVOID)lpPassword, dwSize, &dwBytesRead, 0))
{
if (dwBytesRead == dwSize)
bRET = TRUE;
}
}
CloseHandle(hFile);
}
}
return bRET;
} |
_________________
|
|
| Back to top |
|
 |
crayzbeef Expert Cheater
Reputation: 0
Joined: 21 Jan 2007 Posts: 101
|
Posted: Tue Jan 20, 2009 2:39 pm Post subject: |
|
|
| lurc wrote: | Well the warning is telling you exactly whats wrong. Your comparing a signed integer to a unsigned integer. Because you initialized i as an int, while strlen returns an unsigned int.
Second is because char * isn't a struct/class/union. You are confusing yourself with the type string, which is a class that deals with characters for you. It has inline functions like that. Anyways your better off just doing something like this:
| Code: | BOOL GetPassword(__in_z LPTSTR lpFileName, __out_z LPTSTR lpPassword)
{
HANDLE hFile;
DWORD dwSize, dwBytesRead;
BOOL bRET = FALSE;
if (lpPassword != NULL)
{
hFile = CreateFile(lpFileName, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile != INVALID_HANDLE_VALUE)
{
dwSize = GetFileSize(hFile, 0);
if (dwSize != INVALID_FILE_SIZE)
{
if (ReadFile(hFile, (LPVOID)lpPassword, dwSize, &dwBytesRead, 0))
{
if (dwBytesRead == dwSize)
bRET = TRUE;
}
}
CloseHandle(hFile);
}
}
return bRET;
} |
|
Yes because he totally asked for win32.
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Tue Jan 20, 2009 10:28 pm Post subject: |
|
|
| crayzbeef wrote: | | lurc wrote: | Well the warning is telling you exactly whats wrong. Your comparing a signed integer to a unsigned integer. Because you initialized i as an int, while strlen returns an unsigned int.
Second is because char * isn't a struct/class/union. You are confusing yourself with the type string, which is a class that deals with characters for you. It has inline functions like that. Anyways your better off just doing something like this:
| Code: | BOOL GetPassword(__in_z LPTSTR lpFileName, __out_z LPTSTR lpPassword)
{
HANDLE hFile;
DWORD dwSize, dwBytesRead;
BOOL bRET = FALSE;
if (lpPassword != NULL)
{
hFile = CreateFile(lpFileName, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile != INVALID_HANDLE_VALUE)
{
dwSize = GetFileSize(hFile, 0);
if (dwSize != INVALID_FILE_SIZE)
{
if (ReadFile(hFile, (LPVOID)lpPassword, dwSize, &dwBytesRead, 0))
{
if (dwBytesRead == dwSize)
bRET = TRUE;
}
}
CloseHandle(hFile);
}
}
return bRET;
} |
|
Yes because he totally asked for win32. |
He also never stated he didn't want Win32 .
You do know that those classes will make calls to CreateFile, ReadFile, etc. anyways.
Seriously what the fuck is your problem, literally every post you make is aggressive or just stupid.
_________________
|
|
| Back to top |
|
 |
123qwe Newbie cheater
Reputation: 0
Joined: 26 Dec 2006 Posts: 21
|
Posted: Wed Jan 21, 2009 12:36 am Post subject: |
|
|
| its ok guys, no need to argue, i appreciate both of your replies, so thanks! =]
|
|
| Back to top |
|
 |
crayzbeef Expert Cheater
Reputation: 0
Joined: 21 Jan 2007 Posts: 101
|
Posted: Wed Jan 21, 2009 1:38 am Post subject: |
|
|
| lurc wrote: | | crayzbeef wrote: | | lurc wrote: | Well the warning is telling you exactly whats wrong. Your comparing a signed integer to a unsigned integer. Because you initialized i as an int, while strlen returns an unsigned int.
Second is because char * isn't a struct/class/union. You are confusing yourself with the type string, which is a class that deals with characters for you. It has inline functions like that. Anyways your better off just doing something like this:
| Code: | BOOL GetPassword(__in_z LPTSTR lpFileName, __out_z LPTSTR lpPassword)
{
HANDLE hFile;
DWORD dwSize, dwBytesRead;
BOOL bRET = FALSE;
if (lpPassword != NULL)
{
hFile = CreateFile(lpFileName, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile != INVALID_HANDLE_VALUE)
{
dwSize = GetFileSize(hFile, 0);
if (dwSize != INVALID_FILE_SIZE)
{
if (ReadFile(hFile, (LPVOID)lpPassword, dwSize, &dwBytesRead, 0))
{
if (dwBytesRead == dwSize)
bRET = TRUE;
}
}
CloseHandle(hFile);
}
}
return bRET;
} |
|
Yes because he totally asked for win32. |
He also never stated he didn't want Win32 .
You do know that those classes will make calls to CreateFile, ReadFile, etc. anyways.
Seriously what the fuck is your problem, literally every post you make is aggressive or just stupid. |
Oh sorry for me getting mad when every C++ topic i see people reply with win32 shit.
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Wed Jan 21, 2009 3:07 am Post subject: |
|
|
I am glad that your skills are at a level of which is completely incomprehensible to us mere plebes.
So why have I never seen one useful post coming from you? Or any code for that matter?
|
|
| Back to top |
|
 |
Snootae Grandmaster Cheater
Reputation: 0
Joined: 16 Dec 2006 Posts: 969 Location: --->
|
Posted: Wed Jan 21, 2009 3:37 am Post subject: |
|
|
| Quote: | | Oh sorry for me getting mad when every C++ topic i see people reply with win32 shit. |
that couldnt possibly be cos thats what most people seem to use, and understand
_________________
|
|
| Back to top |
|
 |
crayzbeef Expert Cheater
Reputation: 0
Joined: 21 Jan 2007 Posts: 101
|
Posted: Wed Jan 21, 2009 1:45 pm Post subject: |
|
|
| Snootae wrote: | | Quote: | | Oh sorry for me getting mad when every C++ topic i see people reply with win32 shit. |
that couldnt possibly be cos thats what most people seem to use, and understand |
Then why are they replying to a C++ topic posting it.
| slovach wrote: | I am glad that your skills are at a level of which is completely incomprehensible to us mere plebes.
So why have I never seen one useful post coming from you? Or any code for that matter? |
Maybe because all the shit here is hacking? I just visit here to see if any intelligent things are being programmed. Nothing yet.
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Wed Jan 21, 2009 2:37 pm Post subject: |
|
|
Are you stupid? Like really... Win32 is just using Window API in C++. It is still using the C++ language.
Which all classes do, with the exception of the CRT. They act as wrappers!
Then if you think your so high and mighty how about you use your unbelievably stupid intelligence and program something so impressive that will knock us off our feet.
Yea, come back and call all this shit when you can prove you know something .
_________________
|
|
| Back to top |
|
 |
|