LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-06-2012, 08:37 AM   #1
GamezR2EZ
Member
 
Registered: Jan 2009
Posts: 31

Rep: Reputation: 4
C - Open varying number of files dynamically


I have run into a bit of an impasse with a project I am working on. I need to open up a varying number of files and compare them to one another in sections.

The code below works and does a basic version of what I need. I wrote this quickly:

Code:
int main(int argv, char *argc[])
{
  int A, B, i, i2, x, sz1, sz2; //2 separate bytes
  unsigned char buffer1[2532]; 
  unsigned char buffer2[2532];
  FILE *test1, *test2;
  
  if(argv <= 1){
    printf("This program needs a minimum of 2 files\n");
    return 1;
  }
  
  test1 = fopen(argc[1], "rb");
  test2 = fopen(argc[2], "rb");
  
  fseek(test1, 0L, SEEK_END);
  sz1 = ftell(test1);
  fseek(test1, 0L, SEEK_SET);
   
  fseek(test2, 0L, SEEK_END);
  sz2 = ftell(test2);
  fseek(test2, 0L, SEEK_SET);
  
  int numFrames = (sz1/2352);
  
  bool framesdiff[numFrames];
  
  for(i=0; i<(numFrames); i++){
    framesdiff[i] = 0;
  }
  
  for(i=0; i<(numFrames); i++){
    fread(&buffer1, 2532, 1, test1);  
    fread(&buffer2, 2532, 1, test2);
  
    for(i2=0; i2<2532; i2++){
      if(buffer1[i2] == buffer2[i2])
      {
	//printf("They match!\n");
      } else {
	framesdiff[i] = 1;
	break;
	//printf("He's dead Jim!\n");
      }
    }
    //printf("%X\n", framesdiff[i]);
  }
  
  for(i=0; i<(numFrames); i++){
    if(framesdiff[i] == 1){
      printf("Frame %d differs\n", i+1);
    }
  }
  
  //printf("Size 1: %d\nSize 2: %d\n%d %d\n", sz1, sz2, i , i2);
  
  fclose(test1);
  fclose(test2);
  
  return 0;
}
Now I need to open a varying number of files and do essentially the same thing. The comparing part I have not worked on, the opening multiple files I need help with. I just don't see the code. The only thing I can figure would be something like this (I know this will not work):

Code:
for(i=1;i<argv;i++){
  FILE *fp(i) = fopen(argc[1], "rb");
}
Where the FILE variables would be fp1, fp2, fp3, ect..

Now I am aware that will not work, but it demonstrates my current approach.

Please give me some ideas of how to proceed. This is a learning project for me, so I also appreciate direction instead of outright answers if you have any advice (I can learn from both).

The platform I am working on is Linux.

---Solution---

I was just tired.

Code:
FILE *fp[argv-1];

for (i=0; i<(argv-1); i++){
  fp[i] = fopen(argc[i+1], "rb");
  fclose(fp[i]);
}

Last edited by GamezR2EZ; 03-07-2012 at 06:43 AM.
 
Old 03-06-2012, 09:24 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
correction:

Code:
FILE *f[argc-1];

for (i=0; i<argc-1; i++) {
    f[i] = fopen (argc[i+1], "rb");
    if (! f[i]) /* error */
}

Last edited by NevemTeve; 03-06-2012 at 09:40 AM. Reason: argv[0] isn't an actual parameter
 
1 members found this post helpful.
Old 03-07-2012, 06:42 AM   #3
GamezR2EZ
Member
 
Registered: Jan 2009
Posts: 31

Original Poster
Rep: Reputation: 4
Thanks, that's it. That is what I get for working on a problem when I am to tired to stand.
Also, while argv[0] (as an array) isn't correct argv (as an int) most certainly is. It holds the number of parameters starting at 1.

Good news is I got the comparing part done while I took a break from this simple solution.

Found your post helpful, updated first post, marked as solved, ect.

Code:
FILE *fp[argv-1];

for (i=0; i<(argv-1); i++){
  fp[i] = fopen(argc[i+1], "rb");
  fclose(fp[i]);
}
 
Old 03-07-2012, 07:15 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Off:

Well, you use 'argc' and 'argv' in an unique way, the general usage is:
Code:
int main (int argc, char *argv[]);
c=count, v=vector
 
1 members found this post helpful.
Old 03-07-2012, 08:19 AM   #5
GamezR2EZ
Member
 
Registered: Jan 2009
Posts: 31

Original Poster
Rep: Reputation: 4
I definitively do, I have been messing around with C for years and learned argc/argv backwards. Now I never remember what is the correct (read: standard) way. I always just reference the way it is written in the program I am working on.

I will try to remember v=vector in the future, but old habits die hard.

I either case, this program is doing exactly what I needed!

Thanks again.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to determin the number of 'Open Files'? thomas2004ch Linux - Newbie 4 11-18-2009 02:54 AM
Determine number of checklist in zenity dynamically... shivarajM Linux - Software 3 05-02-2009 06:03 AM
commands for bash script that handles files of varying number of lines BBFeltham Linux - Newbie 1 07-26-2008 10:18 AM
Number of Open Files rjapenga Linux - General 2 04-14-2005 01:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration