Thread: redirecting confusion

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    192

    redirecting confusion

    Okay so I'm not really sure what I'm looking for but I can't find an example to figure it out.

    So if I had a directory with a bunch of files could be text files. the directory is text.
    and on linux I want to make a program that reads all the files in the directory so I want to use the redirect command on linux

    myprog < text

    what does my program to have to read the files in the directory "text"?
    like is there an example because all the redirect commands all have examples redirecting files but I want to redirect a directory.
    I need a C program to read in the directory Usually reads it in with a struct like

    FILE *in

    is there like a struct for directorys?

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You can't do precisely what you want. Instead, pass the name of the directory as an argument and use opendir()/readdir(), or perhaps scandir(), to read the directory entries.

  3. #3
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    How about
    Code:
    myprog < text/*
    that'll use bash to go through all the files in the directory

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    Thanks cas I going to go towards that directions I actually had one more question about redirection

    myprog < file1

    what structure can take the file in is it part of stdin and i would need to use fgets

    / or is it like
    FILE in*
    and in is "file1"

    yea im just really not sure how to use < and having a hard time finding some tutorial thats useful

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well if you mean what data type is the argument you pass to the program, it is a char array. You need to declare a FILE pointer that points to the file named argv[1] as such:

    Code:
     
    int main(int argc, char *argv[]){
       
       FILE *fp;
       fp = fopen(argv[1],"r");
    
       return 0;
    }
    So, if you run your program as:

    ./myprog myfile.txt

    argv[1] will contain "file.txt".

    If you redirect what bernt said, then you will simply get a huge text of all the files and folders in that dir:

    i.e. ./myprog < text/*

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by claudiu View Post
    If you redirect what bernt said, then you will simply get a huge text of all the files and folders in that dir:
    No you won't. Redirection doesn't work that way. Redirection takes a SINGLE file and sends it to stdin. Listing multiple files doesn't do what you're saying.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    okay so im still kinda of confused on how to do this

    myprog < file1.txt

    so im assuming stdin is the connection to FILE stream for file1.txt
    but how to i create the program to store in a string variable file1.txt
    without knowing its already file1.txt so it could be any random txt file but i want my program to store it on a char* variable the title of the file?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. redirecting confusion
    By kiros88 in forum Linux Programming
    Replies: 1
    Last Post: 03-02-2010, 08:44 PM
  2. Pointer confusion. Please explain :)
    By nichya88 in forum C++ Programming
    Replies: 7
    Last Post: 01-26-2010, 12:18 PM
  3. a Bit of confusion (haha - Pun)
    By Junior89 in forum C++ Programming
    Replies: 1
    Last Post: 06-15-2007, 11:22 PM
  4. for loop confusion
    By Enges in forum C++ Programming
    Replies: 6
    Last Post: 04-26-2006, 08:21 AM
  5. Unicode - a lot of confusion...
    By Jumper in forum Windows Programming
    Replies: 11
    Last Post: 07-05-2004, 07:59 AM