Thread: Help with reading a file

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    7

    Help with reading a file

    if I have a file, marks.dat:
    8
    5 9 3 7 8 7 8 9

    How to I read the first line(8) and put it into num_marks, and put the rest of the numbers into an array (marks[])?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. Read the first number and call it num_marks.
    2. For i = 0 to (num_marks-1): Read in a number and call it marks[i].

    Notice that you won't be able to use num_marks as an array size in the declaration of marks[], so you'll need to either pick a "large enough" number or use malloc().

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    7
    I still don't completely understand what to do. I'm kinda new with this stuff, so if you could be a bit more specific that would help, thanks.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    Code:
    //---------------------------------------------------------------------------
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define One 1
    #pragma hdrstop
    
    //---------------------------------------------------------------------------
    
    /* Read some data from a binary file into an array. */
    
    void FillArrayFromBinaryFile(const char *filename, int **ptrArray, int *ArraySz)
    {
    	if(!filename)
    	{
    		fprintf(stderr, "NULL input filename.\n");
    		return;
    	}
    	else
    	{
    		FILE *filePtr = NULL;
    		if((filePtr = fopen(filename, "rb")) == NULL)
    		{
    			fprintf(stderr, "Error Opening:\"%s\" file.", filename);
    			return;
    		}
    		else
    		{
    		unsigned int Index = 0;
    		int *nArray = calloc(One, sizeof(int));
    		if(!nArray)
    		{
    			fprintf(stderr, "Memory Error.\n");
    			fclose(filePtr);
    			return;
    		}
    			for(;;)
    			{
    			   if(1 != fread(&nArray[Index], sizeof(int), 1, filePtr))
    				break;
    			   /* Each time. */
    			   Index++;
    			   nArray = realloc(nArray, Index+1 * sizeof(int));
    			   fseek(filePtr, Index * (sizeof(int)+sizeof(char)), SEEK_SET);
    			}
    			*ptrArray = nArray;
    			*ArraySz = Index;
    			fclose(filePtr);
    			return;
    		}
    	}
    }
    void PrintArray(int *nArray, int Sz)
    {
    	static int Index = 0;
    	for(Index = 0; Index < Sz; Index++)
    		printf("%d ", nArray[Index]);
    	printf("\n");
    }
    void WriteArrayToBinaryFile(const  char *filename, int *nArray, int nSz)
    {
    	if(!filename)
    		return;
    	else
    	{
    		FILE *filePtr = NULL;
    		if((filePtr = fopen(filename, "wb")) == NULL)
    		{
    			fprintf(stderr, "Error Opening:\"%s\" file.", filename);
    			return;
    		}
    		{
    			int Index  = 0;
    			char cChar = ' ';
    			for(Index = 0; Index < nSz; Index++)
    			{
    				fwrite(&nArray[Index], sizeof(int), 1, filePtr);
    				fwrite(&cChar, sizeof(char), 1, filePtr);
    			}
    			fclose(filePtr);
    			return;
            }
        }
    }
    
    #pragma argsused
    int main(int argc, char* argv[])
    {
    	int nArraySz = 0;
    	int nArray[4];
    	int *ptrToArray = NULL;
    	nArray[0] = 1;
    	nArray[1] = 4;
    	nArray[2] = 6;
    	nArray[3] = 12;
    	WriteArrayToBinaryFile("Array.dat", nArray, 4);
    	memset(nArray,0, sizeof(nArray));
    	FillArrayFromBinaryFile("Array.dat", &ptrToArray, &nArraySz);
    	PrintArray(ptrToArray, nArraySz);
    	free(ptrToArray); ptrToArray = NULL;
    	printf("Hit enter to continue...\n");
    	getchar();
    	return 0;
    }
    //---------------------------------------------------------------------------
    The above code will not solve your problem, but will give you a point to start. It writes an array to a binary file, then sets the array elements to zero and try to read them back again from the binary file. The difference is that using realloc i dont care what is the sizeof the array(number 8) in the binary file.

    I just write to the file:

    1 4 6 12

    and try to get them back again.

    *Possible bugs included. *

    Bokarinho.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM