Helpful Information
 
 
Category: C++
C++ >> Whats the Best way to Open/Read/Write Files for a text editor

I have herd multiple ways and not sure which one is best.

heres the ways i know:

fstream(ofstream,ifstream,fstream) type
CreateFile() functions
fopen(),fclose(),exc... functions


but i need one that will work with GetOpenFileName/GetSaveFileName because i'm making a text editor. but i can't get fstream or fOpen() ones to work with it and When i do CreateFile and try to read from the file it doesn't work.

Why? What happened? You aren't using CBasic are you? :confused:

Posting "it doesn't work" isn't helpful when you don't show your code.

For example, are you making allowances for the lpstrFile member of the OPENFILENAME struct being in UNICODE or ANSI?

Did you actually allocate the space for this pointer, or did you rely on magic?

What about checking error returns?

I didn't post code because that wasn't the qeustion i was asking.

and i got it to work. although how do i read an entire file?

i tried setting nNumberOfBytesToRead to GetFileSize(File, NULL) but that puts abunch of characters similar to a "i" and then the file path at the end of the file

heres my read file code


ReadFile(File, FileContents, GetFileSize(File, NULL), NULL, NULL)



You aren't using CBasic are you?

Haha No.

I didn't post code because that wasn't the qeustion i was asking.
Yeah, and your question has no answer. There is no universal best technique or panacea to compensate for sloppy programming. It depends on needs of your application - which you have not specified and, given the way you're obviously hacking, I'm not particularly interested in exploring.

As to your code not working;

1) GetFileSize() can fail. If it does, you need to take recovery action, not pass it's return value to ReadFile().

2) ReadFile() assumes you pass the address of a valid buffer for it to write to. It doesn't magically create a buffer to read into. Your code implicitly assumes FileContents is a buffer sufficient to hold the whole file.

In other words, you're not using these functions in a manner consistent with what their documentation describes.

Whichever set of functions you decide to use to read the file, you need to ensure you actually read the documentation and use them correctly. Otherwise all three options (C++ streams, C I/O, or win32 API) will fail to do what you want.

Well I guess you're on your own until you post more than one line of code completely out of context.

Post something which doesn't work (but you'd like it to), and then we can tell you how to fix it.

Yeah, and your question has no answer. There is no universal best technique or panacea to compensate for sloppy programming. It depends on needs of your application

i really wasn't looking for a universal best. i was more looking for oppinion on whats works good. as for the needs of my app. i could have sworn i put in there i'm new and am just trying to make a simple text editor


- which you have not specified and, given the way you're obviously hacking, I'm not particularly interested in exploring.

I'm not hacking?????!!!!!!! Read Above lol. sorry for the confusion.


As to your code not working;

1) GetFileSize() can fail. If it does, you need to take recovery action, not pass it's return value to ReadFile().

2) ReadFile() assumes you pass the address of a valid buffer for it to write to. It doesn't magically create a buffer to read into. Your code implicitly assumes FileContents is a buffer sufficient to hold the whole file.

In other words, you're not using these functions in a manner consistent with what their documentation describes.

Whichever set of functions you decide to use to read the file, you need to ensure you actually read the documentation and use them correctly. Otherwise all three options (C++ streams, C I/O, or win32 API) will fail to do what you want.



1>> Ya i fixed that already.
2>> I am sorry. FileContents is a valid buffer. i just didn't post that part. Would you like the hole cpp file?
3>> I have read the documentation on msdn/cplusplus.com


and sorry for the top quotes being in code boxes. Whoops.

I'm not hacking?????!!!!!!! Read Above lol. sorry for the confusion.
'Hacking' in the sense of "coding carelessly or without consideration for good technique", i.e., just throwing some code on the screen and hoping it works rather than trying to figure out the details. Despite what the press seems to think, the word has quite a few different meanings regarding programming, and "electronic B&E" is not the most common sense which programmers use.

'Hacking' in the sense of "coding carelessly or without consideration for good technique", i.e., just throwing some code on the screen and hoping it works rather than trying to figure out the details. Despite what the press seems to think, the word has quite a few different meanings regarding programming, and "electronic B&E" is not the most common sense which programmers use.

Oh. ok. i was confused for a sec sorry.

I am not "Coding Carelessly" i am just trying to learn? thats why i asked lol. i had looked at the documentation for the commands and was just trying to figure out which one was the best for a text editor(in other peoples oppinion). as that is what i'm trying to make. by the way. i have settled on Win32 API way as that way i can just use GetFileSize to read the hole file.

I got it to read and write files. then i was trying to make it so if there was a file open and you press save it would save it(not bring up the GetSaveFileName() dialog like before)

well it's not Opening the File. what i'm doing is Closing it then truncating it and writing everything from the edit control back to it. well thats what i'm trying to do. heres the code for if the user presses save in the menu(and theres a file open)


CloseHandle(File);
File = CreateFileA(FilePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, TRUNCATE_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (File == INVALID_HANDLE_VALUE){
MessageBoxA(NULL, "Could Not Create File", "CreateFileA()", MB_OK)
break;
}
GetWindowTextA(Edit, lpString, GetWindowTextLength(Edit));
if (WriteFile(File, lpString, GetWindowTextLength(Edit) + 1, NULL, NULL) == FALSE)
MessageBoxA(NULL, "Could Not Write File", "WriteFile()", MB_OK);

FilePath was stored earlier as the full file path of the file the user selected when the user pressed Open or save as(or save when theres no file open) and File is the handle to the file.

It always fails to Open the file. i even tried taking some CreateFile code that worked and using it and it didn't work. i have also checked all through it and theres no where that FilePath could have been changed that i could find.