How would I be able to do this?

This is a discussion on How would I be able to do this? within the C Programming forums, part of the General Programming Boards category; HI, I am a noob, and I was just wondering if there was a way to take a text in ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    13

    How would I be able to do this?

    HI, I am a noob, and I was just wondering if there was a way to take a text in the same directory as the project files/exe.. And print it in a console window...
    For example)
    I have a text file name text.txt
    Text.txt has the words "hello world"


    how could I make the program grab that text.txt files characters "hello world" and print it in the console window?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int n;
        while ((n = getchar()) != EOF) {
            putchar(n);
        }
        return 0;
    }
    This is how I would do it, and this is how I would run the program (on Windows):

    Code:
    myprogram < Text.txt


    But I suppose you'd have to do it with file operations. You might start with I/O functions, particularly with fopen.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    You should read the tutorial on this site for file i/o. That will have everything you need.
    -- Will you show me how to c++?

Popular pages Recent additions subscribe to a feed

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