get input from a file

This is a discussion on get input from a file within the C++ Programming forums, part of the General Programming Boards category; hi: Did you know how to put value into n by 3 array from a file called "inputfile.txt". Can anyone ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    3

    get input from a file

    hi:

    Did you know how to put value into n by 3 array from a file called "inputfile.txt". Can anyone please show me how to do it, thank you. The compiler I am using is dev c++ version 4

    The "inputfile.txt" contain following data

    2a 3b 6d
    6s 6c 6h
    ....
    ...
    Last edited by hnry; 11-19-2003 at 01:06 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,313
    Yes, I knew how

    Do you know how to read in a single value from a text file in the first place?

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    3
    do I need to include the #include <fstream> header.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    using namespace std;
    
    int main()
    {
     const int COL=3;
     ifstream infile ("input.txt");
     char array[n][COL];//array always have 3 column
     
     //put value into array
                ......
                ......
     system("pause");
     return 0;
    }//end main

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,313
    Yes, you would need to include <fstream>

    This is what I suggest:
    1. Write the code to open the file, read 1 string, and close the file.
    You will have to decide if you're going to use strings of 2 character + '\0', or C++ strings using <string>

    2. Extend the code to read in 3 such strings.
    This would be in your array.
    You will have to decide if you're going to use a normal array, or #include <vector> and use such dynamic arrays.

    3. Extend the code to deal with the whole file.
    If you're using dynamic arrays the transition shouldnt be a problem, otherwise you'll have to start using them, or use dynamic memory allocation yourself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  4. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

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