Thread: Reading from a text file with stdin

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    14

    Reading from a text file with stdin

    Hello, my project asks that I read input from stdin that allows redirection from a text file. Then redirect the output to a different file using stdout . It mentions that can't use file operations to read the input. The text file I have is an 9x9 array of characters separated by spaces. I already set up a code that will convert it to a 2D array. But I'm not sure how to do the input/output correctly, currently I am using ifstream for reading the file.
    (Sorry for the mess of a code)
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <fstream>
    #include <vector>
    #include <array>
    using namespace std;
    int main()
    {
    
     
        //Open text file
       ifstream file("letters.txt");
    
        string line;
        int row = 0;
        int col;
        char array[25][25];
        // Insert each character into array
        while (getline(file, line))
        {
            istringstream iss(line);
            char value;
            col = 0;
    
            while (iss >> value)
            {
                array[row][col] = value ;
                col++;
            }
            row++;
        }
    
    
    
        cout << "********PRINT OUTPUT******" << endl;
        for (int i = 0; i < row; ++i)
            {
                for (int j = 0; j < col; ++j)
                {
                    cout << array[i][j] << ' ';
                }
                cout << endl;
            }
       
    }
    Last edited by Befiscure45; 09-01-2019 at 01:26 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,445
    Hello, my project asks that I read input from stdin that allows redirection from a text file.
    So do you understand this "redirection" thing that is being talked about above?

    It mentions that can't use file operations to read the input.
    Yes, because "redirection" means that on the command line you told the program to use a file on disk for input instead of using the "normal" standard input. By the way what exactly does your input file look like?

    I already set up a code that will convert it to a 2D array.
    Well your first task should be to read the data correctly, by using cin (probably) with extraction operator>>.

    But I'm not sure how to do the input correctly, currently I am using ifstream for reading the file.
    (Sorry for the mess of a code)
    Why are ou using an ifstream? Your instructions state you can't use "file" operations. You need to read the input from std::cin.

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    14
    Quote Originally Posted by jimblumberg View Post
    Why are ou using an ifstream? Your instructions state you can't use "file" operations. You need to read the input from std::cin.

    The ifstream is just temporary. At least to read the text file correctly. But here is one that does use cin in it with a commandline argument. Should this be the model I should use?
    Code:
    void PL(std::istream& is) {
       string line;
    
        while(is) {
            cout<<"Type line now";
            if(std::getline(is,line)) {
                // supposed to Parsing string into words and translate//
                //but just reading back input for now//
                cout<<"You typed:"<<line<<endl;
            }
        }
    }
    
    int main(int argc, char* argv[]) {
    
        std::istream* input = &std::cin; // input is stdin by default
        if(argc > 1) {
            // A file name was give as argument,
            // choose the file to read from
            input = new std::ifstream(argv[1]);
        }
    
        PL(*input);
    
        if(argc > 1) {
            // Clean up the allocated input instance
            delete input;
        }
        return 0;
    }

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,445
    But here is one that does use cin in it with a commandline argument.
    No it doesn't really use cin and it shouldn't be using a command line argument.

    Should this be the model I should use?
    No. Something more like:

    Code:
    #include <fstream>
    #include <iostream>
    
    void get_input(std::istream& fin);
    
    int main()
    {
        
        get_input(std::cin);
        
    }
    
    void get_input(std::istream& fin)
    {
        int data;
        while(fin >> data)
            std::cout << data << ' ';
        
        std::cout << '\n';
    }
    Of course this is just a guess since you haven't posted your input file yet.

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    14
    Quote Originally Posted by jimblumberg View Post
    Of course this is just a guess since you haven't posted your input file yet.
    Sorry. Here is the input file I will be using. chocolate.txt

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,445
    And what are you going to do with that data?

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    14
    Quote Originally Posted by jimblumberg View Post
    And what are you going to do with that data?
    I've already mentioned in one of my posts. But I need to convert it to a 2D array where each new line is the first dimension of the array(row), and every char on that line is saved into the second dimension(column).

  8. #8
    Registered User
    Join Date
    Apr 2019
    Posts
    14
    Sorry if this doesnt make sense. I have posted a better question here Reading a text file using stdin then traversing through multiple arrays

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading and writing to stdin, stdout and file
    By Quinhagen in forum C Programming
    Replies: 2
    Last Post: 05-25-2016, 07:39 AM
  2. reading from stdin and FILE
    By allen. in forum C Programming
    Replies: 2
    Last Post: 09-12-2014, 11:57 PM
  3. Reading From a text file
    By jian in forum C Programming
    Replies: 6
    Last Post: 12-31-2010, 04:43 AM
  4. Replies: 8
    Last Post: 05-05-2010, 02:43 PM
  5. reading text from a file
    By GSLR in forum C Programming
    Replies: 5
    Last Post: 05-13-2003, 03:27 PM

Tags for this Thread