how to create an array of pointers to files

This is a discussion on how to create an array of pointers to files within the C Programming forums, part of the General Programming Boards category; hi all i want to create an array of files. but the size of the array is not given initially, ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    10

    how to create an array of pointers to files

    hi all
    i want to create an array of files. but the size of the array is not given initially, but will be read from user input. i tried this but it didn't work.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(){
    	
    	FILE *fp;
    	int t;
    	int i;
    	char filename[50];
    
    	t = 8; //will be read from user input actually
    	
                    fp = malloc (t * sizeof(FILE*) );
    	
    	for ( i=0; i<t; ++i){
    	                sprintf (filename, "%d.txt", i);	
    		fp[i] = fopen("filename", "w");
    	}
    	return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    Try
    FILE **fp;

    Be aware that many platforms have very low limits (compared to everything else) on the number of files you can have open at the same time.
    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.

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    I believe you need a double FILE pointer. Try:
    Code:
    FILE **fp;
    edit: beaten
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    10
    thanks a lot

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    10
    another problem
    i want to partition a text file into several files with the below code but each new created file only contains last partition of the input file
    Code:
    int main(){
    
    	FILE* inputFile;
    	FILE** fpArray;
    
    	int t;
    	int i;
    	int j;
    	char filename[50];
    	char line[LINE_LENGTH];
    	int numOfLines = 0;
    
    	inputFile = fopen("sample.text", "r");
    
    	//count lines
    	while( fgets(line, sizeof(line), inputFile) ){
    		 ++numOfLines;
    	}
    
    	t = 8; //number of partitions
    	fpArray = malloc( t * sizeof(FILE*) );
    	//create files
    	for( i=0; i<t; ++i){
    		sprintf(filename, "%d.txt", i);
    		fpArray[i] = fopen(filename, "w");
    	}
    
    	for ( i=0; i<t; ++i ){
    
    		for( j=0; j<(numOfLines/t); ++j ){
    
    			//fgets(line, sizeof(line), inputFile);
    			fscanf(inputFile, "%s", line);
    			fprintf(fpArray[i], "%s", line);
    		}
    
    	}

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    > while( fgets(line, sizeof(line), inputFile) )
    When you've counted the lines, you need to set the file pointer back to the start of the file.

    Code:
    rewind( inputFile );
    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.

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    10
    once again thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-02-2006, 11:41 AM
  2. Creating an array of pointers to int[]
    By OkashiiKen in forum C Programming
    Replies: 3
    Last Post: 09-29-2006, 07:48 PM
  3. how to create an array of pointers
    By Dan17 in forum C++ Programming
    Replies: 12
    Last Post: 04-26-2006, 09:02 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. Replies: 1
    Last Post: 07-24-2002, 07:33 AM

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