![]() |
| | #1 |
| Registered User Join Date: Feb 2002
Posts: 9
| multi file i/o 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);
}
}
|
| Screamager is offline | |
| | #2 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| 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);
}
}
|
| Salem is offline | |
| | #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? |
| Screamager is offline | |
| | #4 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > 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 |
| Salem is offline | |
| | #5 | |
| Registered User Join Date: Feb 2002
Posts: 9
| The error message on my tacky c++ compiler is Quote:
| |
| Screamager is offline | |
| | #6 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > 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++. |
| Salem is offline | |
| | #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! |
| Screamager is offline | |
| | #8 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > 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 |
| Salem is offline | |
| | #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! |
| Screamager is offline | |
| | #10 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| 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;
}
Then you can figure out what it takes to make your compiler behave as you want/expect it to |
| Salem is offline | |
| | #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! |
| Screamager is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Data Structure Eror | prominababy | C Programming | 3 | 01-06-2009 09:35 AM |
| File I/O Assertion Failure in VS2008 | clegs | C Programming | 5 | 12-25-2008 04:47 AM |
| Game Pointer Trouble? | Drahcir | C Programming | 8 | 02-04-2006 02:53 AM |
| Unknown Memory Leak in Init() Function | CodeHacker | Windows Programming | 3 | 07-09-2004 09:54 AM |
| advice on file i/o | Unregistered | C Programming | 1 | 11-29-2001 05:56 AM |