Thread: multi file i/o

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    9

    multi file i/o

    Hi everyone,
    Im trying to write a piece of code which, when the filename is typed by the user, reads in a number of files of the same name with one differing character (a number on the end). Ive started a bit of code now and its not finished or working at all really but I was just wondering if anyone had any tips as what to do next with it because im kinda stuck! Each of the files needs to be read into an array. So for example if I have 5 files that I want read they need to be stored in 5 different arrays. I know the read function im using justnow wont do that. Any suggestions for how I should modify the code?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void read(char[]);
    
    int main(){
    
    FILE *fileHandle;
    int i, j, iNumFiles;
    char sFileName[100];
    
    printf("Please enter the number of files you wish to use: ");
    scanf("%d", iNumFiles);
    
    printf("Please type the name of the file you wish to use: ");
    scanf("%s", sFileName);
    
    for(i=0;i<iNumFiles;i++)
    {
              sprintf(sFileName, "%s_%i.dat", i);
    
              read(sFileName);
              }
         }
    
    void read(char sFileName[]){
    
    FILE* fileHandle;
    int array[64][64];
    int loopCounterX = 0;
    int loopCounterY = 0;
    
    if((fileHandle = fopen(sFileName, "r")) == NULL)
    {
    	printf("Unable to open file : %s\n", sFileName);
    }
    else
    {
              	for(loopCounterX = 0; loopCounterX < 64; loopCounterX++)
                {
    			   for(loopCounterY = 0; loopCounterY < 64; loopCounterY++)
    			   {
    				  fscanf(fileHandle, "%d", &array[loopCounterX][loopCounterY]);
    			   }
    		    }
          fclose(fileHandle);
          }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Something like this ought to work

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void read ( char *filename, int arr[][64] );
    
    int main(){
        int i, iNumFiles;
        char sFileName[100], sBaseName[100];
        int  (*array)[64][64];
    
        printf("Please enter the number of files you wish to use: ");
        scanf("%d", &iNumFiles);
        array = malloc( sizeof(array[0]) * iNumFiles );
    
        printf("Please type the name of the file you wish to use: ");
        scanf("%s", sBaseName);
    
        for(i=0;i<iNumFiles;i++) {
            sprintf(sFileName, "%s_%i.dat", sBaseName, i);
            read(sFileName,array[i]);
        }
        return 0;
    }
    
    void read ( char sFileName[], int array[][64] ) {
        FILE* fileHandle;
        int loopCounterX = 0;
        int loopCounterY = 0;
    
        if((fileHandle = fopen(sFileName, "r")) == NULL)
        {
            printf("Unable to open file : %s\n", sFileName);
        }
        else
        {
            for(loopCounterX = 0; loopCounterX < 64; loopCounterX++)
            {
                for(loopCounterY = 0; loopCounterY < 64; loopCounterY++)
                {
                    fscanf(fileHandle, "%d", &array[loopCounterX][loopCounterY]);
                }
            }
            fclose(fileHandle);
        }
    }

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    9
    Ok that seems logical enough but theres an error with this line "array = malloc( sizeof(array[0]) * iNumFiles );" and thats the only line I dont understand at all! What does it do? And why is it not working?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > but theres an error with this line
    Well that was useful - why didn't you post the error message - this isn't the psychic programming network.

    Lemme guess, its something like
    cannot convert `void*' to `int (*)[64][64]' in assignment
    Or some mutterings about casting void* to non-void*

    Well stop using that tacky C++ compiler, and use a C compiler.

    Perhaps you should read this, so that you know that they are different languages
    http://david.tribble.com/text/cdiffs.htm

    > What does it do?
    It allocates iNumFiles arrays for you, where each array is a [64][64] in size

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    9
    The error message on my tacky c++ compiler is
    ANSI C++ forbids implicit conversion from 'void*' in assignment
    I did run it on an old skool DOS based compiler aswell and it also gave an error but im not sure if that was a C only compiler. I dont have access to it justnow to check the message but it was more along the lines of what you said above. Anyway it shouldnt make a difference! Should it? Ive always thought that agood C-Program would always run on a C++ compiler.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Anyway it shouldnt make a difference! Should it?
    Perhaps - read that link
    Neither is a subset or superset of the other - they have many common features and share a common heritage, but they also have features which are distinct and incompatible.

    > Ive always thought that agood C-Program would always run on a C++ compiler.
    And there are many perfectly valid C programs which break when compiled as C++.

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    9
    Ok I see the problem http://david.tribble.com/text/cdiffs.htm#C99-void-ptr but Im still unclear about how I change it to work in a C++ compiler. Any suggestions?

    handy link by the way, cheers!

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > change it to work in a C++ compiler
    You poor mixed up soul.

    Which language are you programming in?

    Because you keep whining about using C++, yet none of the code you posted made any use of C++ at all - it was all C.

    And since this is on the C board, you're going to get C answers to C questions.

    Now decide which language you're programming in, becuause if you persist with the strange brew of c/c++ (ie neither one thing or the other), you're going to come unstuck.

    The answer is, you use the new and delete operators, not malloc and free

  9. #9
    Registered User
    Join Date
    Feb 2002
    Posts
    9
    I am poor and mixed up! As far as I know im programming in C and thats the way its staying justnow but I dont want to and never expected to have a separate compiler for it. I'll move on to C++ eventually. I think the trouble is that im a physicist and my only basic ("hello world") course in C was taught by physicists who dont really know the difference between C and C++ either!

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Most compiler environments come with both compilers

    Typically, if your source file is a .c file, then it should invoke the C compiler

    If your file is .cc, .cpp, .cxx or anything like that, then it's going to be compiled by the c++ compiler

    Simple test
    Code:
    #include <stdio.h> 
    int main () {
        printf( "This was compiled by C%s\n",
            sizeof('a')==1 ? "++" : "" );
        return 0;
    }
    Just compile and run this simple command line program, to determine whether you're compiling C or C++

    Then you can figure out what it takes to make your compiler behave as you want/expect it to

  11. #11
    Registered User
    Join Date
    Feb 2002
    Posts
    9
    I had it saved as .cpp which is what I was always told to save the files as! Ive chandged it to .c now and its working fine. And to think I always thought those people knew what they were talking about.

    This topic has actually been very educational for me. Thanks for your time!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM