Reading Text files

This is a discussion on Reading Text files within the C++ Programming forums, part of the General Programming Boards category; Is there a way to read a text file if you are using a string from a structure as the ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    88

    Reading Text files

    Is there a way to read a text file if you are using a string from a structure as the text file to open?
    For instance, I have a program that searches for text files and as it searches it stores the name of the text in a structure.
    Later I want to be able to open this text file for reading.
    I have only seen instances where the text file is already given to you.
    like this
    Code:
    readfile = fopen("textfile.txt","r");
    Is there a way I can read using the .name component of this structure
    Code:
    typedef struct 
    {
    char name[30];
    int position;
    int chars;
    int newlines;
    }TXT;
    
    int main(void)
    {
    FILE *readfile;
    Any help, I would appreciate greatly

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Sure if the char name[] field has a string in it you would just use it like:
    Code:
    .
    .
    .
    TXT txt;
    FILE* readfile;
    readfile = fopen(txt.name, "r");
    .
    .
    .
    However this is a C++ board and it should be done with C++:
    Code:
    #include <fstream>
    using namespace std; // shortcut not recommended in real situations
    .
    .
    .
    TXT txt;
    ifstream fin(txt.name);
    .
    .
    .
    You should also consider using the string class in place of char*.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading from text file
    By jamez in forum C Programming
    Replies: 3
    Last Post: 11-30-2005, 07:13 PM
  2. reading text files
    By stimpyzu in forum C++ Programming
    Replies: 11
    Last Post: 04-17-2004, 08:45 AM
  3. Reading spaces, carrage returns, eof from text files
    By thenrkst in forum C++ Programming
    Replies: 1
    Last Post: 03-11-2003, 05:18 AM
  4. reading certain parts of text files
    By Captain Penguin in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2002, 10:45 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21