Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open Text Files

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
As i am a beginner to C++, can some one help me with a peice of code that will open a Text and perform a task on it.
Task: to remove all "." and "," from the text file.

or guide me in write directions.

please help....

thanks.
 
create pointer to file-structure. (FILE *pFile)
open input-file in read-mode (pFile = fopen("the file's name", "rt"))
create pointer to output-file.
open it with "wt".
now get character by character from the input-file and write
it to the output-file if it's not "." or ",".
delete the input file, rename output-file to input-file's name.
Hope this helps.
Sorry that there's no code, but to you as a beginner it's
better that you think about it yourself.
if you use MFC, do the above with class CFILE;
 
I recommend you first learn how to do it with standard library functions like fheyn says above.

However, you can also do it with windows functions. I don't mind showing you code but it is true that you ought to think about it yourself and try to rewrite it without looking at the example if you really want to learn it.

DWORD dwSize, dwBytesRead;
HANDLE hFile = CreateFile("yourfilepath", GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
dwSize = GetFileSize(hFile, NULL);
char* szFileText = new char[dwSize + 1];
szFileText[dwSize] = 0;
if (ReadFile(hFile, szFileText, dwSize, &dwBytesRead, NULL))
MessageBox(0, szFileText, "", 0);
delete szFileText;
}
CloseHandle(hFile);
 
sorry, I forgot to mention that code is only to read the file, from there you will have to figure out how to go through and replace the characters.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top