Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Open an text file

Hi: everybody

Beginner of Windows Program, can anybody tell me how to open, read a text file using Windows programming" code?

Thanks a lot.

James

Comments

  • xikugxikug Member Posts: 1
    : Hi: everybody
    :
    : Beginner of Windows Program, can anybody tell me how to open, read a text file using Windows programming" code?
    :
    : Thanks a lot.
    :
    : James
    :
    You can use ANSI C's files function

    For Examples:
    FILE *fp ; //Define file's Pointer
    fp = fopen("your file", "rw") //Open the file and defined attribute
    //are write and read
  • SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
    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.
    [code]
    char Buffer[4096]; //4kb buffer
    char OneLine[1024]; //For reading up to 1kb of text at once
    FILE *txtfile;

    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!
    [/code]
    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.

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

  • JamesTJamesT Member Posts: 16
    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.

    James

  • SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
    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.
    [code]
    #include
    #include //May or may not be needed, I forget

    char *TextBuffer;
    int TextFile;
    long FileSize;

    if((TextFile = open("Text.txt", "rt")) == -1)
    return -1;

    FileSize = filelength(TextFile);
    TextBuffer = (char*)malloc(FileSize);
    read(TextFile, TextBuffer, FileSize);

    close(TextFile);
    [/code]
    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!
    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

  • Justin BibJustin Bib USAMember Posts: 0

    _______ || http://forcoder.org || free video tutorials and ebooks about || Visual Basic, Java, Swift, Assembly, PHP, C, Ruby, JavaScript, PL/SQL, Go, Objective-C, MATLAB, C++, Visual Basic .NET, Scratch, R, C#, Perl, Python, Delphi Erlang, Apex, LabVIEW, Scala, D, Kotlin, Awk, Hack, Rust, VBScript, Fortran, Clojure, ABAP, ML, Julia, Scheme, F#, Crystal, Transact-SQL, Prolog, Ada, FoxPro, Dart, Alice, Logo, Lisp, SAS, Bash, Lua, COBOL || ______

Sign In or Register to comment.