Thread: Reading from a text file with stdin

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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