Thread: File IO question

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    File IO question

    Hi,

    I have asked this question before but a seam to not understand how it works. So if someone would be so kind to give an example. So what i have is this peace of code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    int main(int argc, char *argv[]){
    
    FILE *fp;
    char buff[91];
    int x,y,c;
    fp = fopen(argv[1], "r");
    
      while (fgets(buff, 10, fp) != NULL)    {
          if (buff[0] == '-'){
              printf("%s",buff);
          }	
          else if (buff[0] == '\n'){
    		  continue;
    	  }
          else if ((c=sscanf (buff,"%d  %d", &x, &y)) !=0){
            printf("%d,%d\n",x,y);
          }
    
      }
     
      fclose(fp);
     return 0;        
    }
    so what it does is it reads some file extracts some info and so on. Also here is the input file.

    Code:
    -first set
    23 22
    32 22
    23 22
    -second set
    54 44
    34 33
    23 45
    23 45
    and this input is saved in file called in.txt

    the way i run the program now is : ./prog in.txt

    but i would like to run it like: cat in.txt | ./prog

    so pipe it from stdout of cat how do i do this ??
    baxy

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Instead of using fopen and fp1, you use stdin for your fgets(). You don't need to fopen() it.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    222
    thnx

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another File question
    By markcole in forum C++ Programming
    Replies: 5
    Last Post: 12-21-2007, 06:03 PM
  2. Newbish Question file reading question....
    By kas2002 in forum C Programming
    Replies: 23
    Last Post: 05-17-2007, 12:06 PM
  3. File IO question
    By CaeZaR in forum C++ Programming
    Replies: 2
    Last Post: 02-07-2006, 02:01 AM
  4. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  5. File question-Testing if file exists
    By fuh in forum C++ Programming
    Replies: 2
    Last Post: 01-28-2003, 07:11 PM