could anybody help me please. I am trying to read a file into 2D, but i couldn\'
ID: 3633585 • Letter: C
Question
could anybody help me please. I am trying to read a file into 2D, but i couldn'tMy textFile look like this
**** |
here is my code
#include <stdio.h>
#include <stdlib.h>
#include "GameOfLife.h"
const int CURRENT_WORLD = 20;
const int NEXT_GENERATION = 40;
int main(void)
{
char print_ch;
char print_G;
int i,j;
char **arr;
char fileName[40];
char organism[CURRENT_WORLD][NEXT_GENERATION];
/*char (*tempPointer)[NEXT_GENERATION];*/
FILE * readFile = NULL;
printf("Please enter the name of file: ");
scanf("%s", fileName);
readFile = fopen(fileName, "r");
while(readFile == NULL)
{
printf("unable to open file. ");
printf(" ");
printf("Please enter the name of the file again: ");
scanf("%s", fileName);
readFile = fopen(fileName, "r");
}
arr = (char **)malloc(CURRENT_WORLD*sizeof(char*));
if (arr == NULL)
{
printf("out of memory ");
exit(1);
}
for (i = 0; i < CURRENT_WORLD; i++)
{
arr[i] = (char*)malloc(NEXT_GENERATION*sizeof(char));
if(arr[i] == NULL)
{
printf("out of memory ");
exit(1);
}
for(j =0; j < NEXT_GENERATION; j++)
{
fscanf(readFile, "%d", organism[i][j]);
}
}
printf("number of Org is %d", organism[i][j]);
for (i = 0; i < CURRENT_WORLD; i++)
{
free(organism[i]);
}
free(organism);
getchar();
getchar();
return 0;
}
what wrong with my code
Explanation / Answer
#include main() { unsigned long a,b; FILE * pRead = fopen("test.dat", "r"); if (pRead==NULL) printf("File cannot be opened "); else printf("Contents of test.dat : "); while ( !feof(pRead) ){ fscanf(pRead,"%u %u ", &a,&b); printf("%u %u ",a,b); } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.