reading a file...

This is a discussion on reading a file... within the C++ Programming forums, part of the General Programming Boards category; Howdy. Just got serious about learning C++ so i decided the best way to learn would be through practice. So ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    11

    reading a file...

    Howdy. Just got serious about learning C++ so i decided the best way to learn would be through practice. So i'm making a progam that takes the tutorials from the home page (cprogramming.com) and displays them when you request them.

    my code:

    //Program is for personal use only. Lessons property of cprogramming.com.

    #include <iostream>
    #include <fstream>


    using namespace std;

    int main()
    {
    char fileName [80];
    char buffer [10000];

    int page;

    cout<<"1.Lessons: \n";
    cout<<"2.Intro to C++ \n";
    cout<<"3.If statements \n";
    cout<<"4.Loops \n";
    cout<<"5.Functions \n";
    cout<<"6.Switch case \n";
    cout<<"7.Pointers \n";
    cout<<"8.Structures \n";
    cout<<"9.Arrays \n";
    cout<<"10.Strings \n";
    cout<<"11.File I/O \n";
    cout<<"12.Typecasting \n";
    cout<<"13.classes \n";
    cout<<"14.Inline functions \n";
    cout<<"15.Command line arguments \n";
    cout<<"16.Linked Lists \n";
    cout<<"17.Recursion \n";
    cout<<"18.Variable argument lists \n";
    cout<<"19.Binary Trees \n";
    cout<<"20.Inheritance \n\n";

    cout<<"Please enter lesson number \n";
    cin>> fileName; //filename is also used as lesson number
    ifstream fin(fileName);
    if (fin)
    {
    cout<<"Current File Contents:\n";
    char ch;
    while (fin.get(ch))
    cout<<ch;
    cout<<"\n***End of File contents.***\n";
    }
    fin.close();
    }

    I have it working to display the file, but it's a .txt file and i need to enter .txt everytime. Is there a way so i can just do the filename without .txt...say add .txt in the code somewhere? Hopefully you guys understand what i'm getting at. Thanks.

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    I'd suggest using the string class, it is easier than C style null terminated character arrays.

    If you use the string class, you could do this:
    Code:
    #include <string>
    //...
    string filename;
    cin >> filename;
    filename += ".txt";
    ifstream fin(filename.c_str());
    If you want to use the C style strings, then do this
    Code:
    #include <cstring>
    //...
    char fileName [80];
    //...
    cin >> filename;
    strcat(filename, ".txt");
    ifstream fin(filename);
    Also, welcome. Please use code tags and read the sticky posts and announcements at the top of the board.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    11
    thanks, i'll try to use this.

    What i know in C++ is very limited. My original thought was to to use if statements, but figured the code would be chaotic. Took me a while to figure it out the way i did it...still not sure if i could do it again

    I'll play around with it. Thanks.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Notice that all I'm doing is appending the string ".txt" to the end of the string inputted by the user. If the user types "17", then the filename will be "17.txt" after the extension is appended. If the user types "HelloWorld", then the string will be "HelloWorld.txt". If the user types "5.txt", then the string will be "5.txt.txt". If the user types "Lesson 1", then the string will be "Lesson.txt", and so on and so forth.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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