The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Extracting data from text file based on configuration set in config file suparnbector Shell Programming and Scripting 3 08-09-2007 11:25 PM
Extracting Data from a File oop UNIX for Dummies Questions & Answers 0 07-31-2007 08:48 AM
Extracting Data from xml file nishana Shell Programming and Scripting 3 07-13-2007 04:17 AM
Extracting data from an AFP file Dolph UNIX for Advanced & Expert Users 4 05-22-2007 01:29 AM
extracting recursive data file bbeugie UNIX for Dummies Questions & Answers 1 06-16-2004 02:48 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #8  
Old 03-20-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 567
Quote:
Originally Posted by ahjiefreak View Post
Hi,

I am executing just by ./binary.exe testfile.txt


where testfile.txt is the filename (contain the number of bits) in the binary.c (2nd argument) passed through argv.
If you are passing a single argument to ./binary.exe then strcpy should have argv[1] as the 2nd argument instead of argv[2].

Code:
strcpy(filename, argv[1]);
Reply With Quote
Forum Sponsor
  #9  
Old 03-20-2008
Registered User
 

Join Date: Dec 2007
Location: Virginia, USA.
Posts: 250
Here's another approach but it's not really pretty.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

#define BSZ 256
#define MAXLINES 5000
#define READLEN 12


int *retData(int num, int offs, char *data, int delim, void *ptr) {
int *retarr = ptr;
char *read_d;

                    if (offs >= num) {return retarr;}
                    if (retarr == NULL) {retarr = malloc(num * sizeof(int)); bzero(retarr,num);}
 
                    
                    if ( (read_d = index(data,delim)) == NULL) {
                        sscanf(data,"%d",&retarr[offs]);
                        return retarr;
                    } else {
                        sscanf(data,"%d",&retarr[offs]);
                        data = read_d + 1;
                    }

                    printf("Debug: Returning data at %p = %d at iteration %d of %d\nData = %p = %s\n",&retarr[offs],retarr[offs],offs,num,data,data);
                    return retData(num,(offs + 1),data,delim,(void *)retarr);
}

int main(int argc, char **argv) {
int x = 0, n, g;
char buf[BSZ];
int *arr[MAXLINES];
FILE *fpt;

         if (argc != 2) {printf("Please specify a filename.\n"); return 1;}
         
         if ( (fpt = fopen(argv[1],"r")) == NULL) {
            printf("Could not access filename %s.\n",argv[1]);
            return 1;
         }

         while (fgets(buf,BSZ,fpt) != NULL) {
               arr[x++] = retData(READLEN,0,buf,',',NULL);
               printf("Node pointer at %p and returned pointer = %p\n",&arr[x],arr[x]);
               if (x == (MAXLINES - 1)) {break;}
         }
         fclose(fpt);

         for (n = 0 ; n < x ; n++) {
             printf("Array node pointer at %p.\n",&arr[n]);
                for (g = 0 ; g < READLEN ; g++) {
                     printf("Values stored at %p %d = %d\n",&arr[n],n,arr[n][g]);
                }
             free(arr[n]);
          }
          return 0;
}
Reply With Quote
  #10  
Old 03-20-2008
Registered User
 

Join Date: Nov 2007
Posts: 131
Hi,

I managed to get them corrected and its running. Thanks alot.

However, I have another question; given that I need to generate alot of these test files. I have a fopen function in my separate main code which write numbers to these file. But I wanted to differentiate these file number.

Do you aware how could we insert number variable in fopen command?
Assume that I have a counter of size 100 whether each round i need to execute below.

e.g
Code:
if((cfPtr = fopen("test%d.txt","a+"))==NULL)
        	               printf("File could not be opened\n");
                else fprintf(cfPtr, "%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d \n", a,b,c,d...);
So that each time the variable counter loops,it will write on different file?

e.g test1, test2,test3.txt...etc. for each different counter counts.

Please advise.
Reply With Quote
  #11  
Old 03-20-2008
Registered User
 

Join Date: Dec 2007
Location: Virginia, USA.
Posts: 250
Use sprintf or strcat and a counter.

Code:
for (i=0 ; i < filecnt ; i++) {
    sprintf(buf,"%s%d.txt",basefilename,i);
    filearr[i] = fopen(buf,"r");
}
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:44 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0