Open an text file
Beginner of Windows Program, can anybody tell me how to open, read a text file using Windows programming" code?
Thanks a lot.
: Hi: everybody
: Beginner of Windows Program, can anybody tell me how to open, read a text file using Windows programming" code?
: Thanks a lot.
You can use ANSI C's files function
FILE *fp ; //Define file's Pointer
fp = fopen("your file", "rw") //Open the file and defined attribute
//are write and read
Fayetteville, NC, USA
Well for an easy to use beginner's example, you could simply define a text buffer you think is big enough for whatever you're trying to read, then just read in lines until EOF.
char Buffer; //4kb buffer
char OneLine; //For reading up to 1kb of text at once
if((txtfile = fopen("blah.txt", "rt")) == NULL)
return -1; //Failed to open blah.txt
//Use a DO or WHILE loop to read the file into
//"OneLine" and append that to the end of "Buffer"
fclose(txtfile); //Close the file handle when done!
That's a very basic way. The more advanced ways include one I used a while back, which gets the size of the file and then allocates a buffer for the text dynamically, and reads the whole file in at once.
Hi: xikug and Sephiroth:
Thank you very much for your help. I feel much better now.
Sephiroth, would you please tell where to find your example about: "gets the size of the file and then allocates a buffer for the text dynamically, and reads the whole file in at once"?
I am very interested in that.
Thank you for your time, appreciated.
Fayetteville, NC, USA
I no longer have the source (lost a LOT a year ago when a Gravis driver caused me to format), but I can post the basics right here.
//May or may not be needed, I forget
if((TextFile = open("Text.txt", "rt")) == -1)
FileSize = filelength(TextFile);
TextBuffer = (char*)malloc(FileSize);
read(TextFile, TextBuffer, FileSize);
That's that! You can use more advanced methods, but that does work. As you can figure out, "filelength()" returns the size of a file (in bytes), and as you probably know, "malloc" allocates memory in bytes to a pointer. Let me know if you need anything else. God, I can't believe I remembered that crap! It's been eons since I coded anything like that, haha!
It looks like you're new here. If you want to get involved, click one of these buttons!
Assembly Code Share
Getting started in assembly
C and C++
C/C++ on Linux/Unix
C/C++ Windows API
C++ Game Development
Delphi and Kylix
Java Server Pages
Access databases and VB
Advance Visual Basic
DirectX Game dev
Newbie Game Programmers
Cooling & Overclocking
Database & SQL
Sound & Music
FreeLance Software City
C# & VB.NET School Support
Join the Team
Comments on this site
New programming languages
Off topic board
Mobile & Wireless
Operating Systems & Platforms
Witsbits Go Cloud
Embedded / RTOS
Windows CE & Pocket PC
Networking And Security
Windows 2003 Server
RUP & UML
Quality & Testing
Active Server Pages
HTML & WEB-Design
Mobile Internet & Messaging
WEB-Services / SOAP
In this Discussion