single File Pointer to Multiple Files

This is a discussion on single File Pointer to Multiple Files within the C Programming forums, part of the General Programming Boards category; Code: int main(int argc, char *argv[]) { int ReadFile=argc-1; while(ReadFile!=1) { FILE *pPoint = fopen (argv[ReadFile],"r"); if (pPoint==NULL) return 0; ...

  1. #1
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294

    single File Pointer to Multiple Files

    Code:
    int main(int argc, char *argv[])
    {
    int ReadFile=argc-1;
    while(ReadFile!=1)
    {
           FILE *pPoint = fopen (argv[ReadFile],"r");
           if (pPoint==NULL)
                      return 0;
    .
    ....
    .... do something
    
    
             fclose (pPoint);					
    	printf("\n\n");
    	ReadFile=ReadFile-1;					
    	fflush(pPoint);
    }
    it does print the first file, after that it gives blank for other files
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  2. #2
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    forgot to reset variable
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,166
    Quote Originally Posted by dayknight
    forgot to reset variable
    What variable?

    Anyway, here's my suggestions...

    First of all, you shouldn't fflush() a closed pointer or a file that's only opened for reading for that matter. I'm not sure why it's there in the first place anyway. Did you add it in for some sort of troubleshooting?

    Secondly, what happens if argc is 1 to start? You'll be looping through all sorts of negative indexes for argv[] which is not good. Use > 0 instead of != 1.

    Thirdly, this loop would be a lot more clear if you used for() instead of while(). Did you mean to show the files in reverse order? Try writing it like this instead:
    Code:
    for(ReadFile = argc - 1;ReadFile > 0;ReadFile--)
    {
      // open the file
      // process it
      // close it
    }
    Last edited by itsme86; 09-15-2005 at 04:30 PM.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Why are you fflush'ing it after you fclose it?
    Your question could use some more information too.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > if (pPoint==NULL)
    In addition to the the flushing a closed file, how about printing an actual message "could not open file", and perhaps use perror() to tell you why you couldn't open a file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  2. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  3. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM
  5. file pointer
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-11-2001, 03:52 PM

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