Hello, I have a question, if you please.

I have been trying to fill an array with values from a text file for hours. I has been 12 years since I have used any C code, and I thought I would be able to remember this. I have an opportunity to use C at work and I am doing a self study tutorial but the code from the book(online) doesn't seem to work properly. Would someone take a look and tell me what I am doing wrong here? I have tried several combinations of things. This is a little program called Clue and it should have only taken me a few hours to do but it has taken me all day to get this far. I am using a DEV_C compiler

My struct

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define ARR_SIZE 100   
#define TRUE 1


 typedef struct {
    char subject[50];
    char relation[50];
    char object[50];
    int string_size;

} FACTS ;

MY fill ARRAY Function

char fillArray(){
     FILE *cFile;
     int i,j,k;
     char *array[ARR_SIZE];
    char c;
      cFile = fopen ("Clue.dat","r"); /*Reopen the file input*/ 
      printf("\n\nThe Array contains: \n\n");
      while((c=getc(cFile))!=EOF) {
      printf("%c",c); 
     // for (i=0; i <ARR_SIZE; i++){ 
    //  strcpy(array[i],&c);
}
//for (i=0; i <ARR_SIZE; i++){
//    c=getc(cFile);
//for (j=0; j < 50; j++){
//    strcpy(array[j],&c);
//for (k=0; k < ARR_SIZE; k++){    
//array[i][j][k]=c;
}
//}

Some lines of clue.dat

Lord Dunsmore?is married to?Lady Emily 
Miss Secrest?called?the Inspector 
The gardener?is married to?the maid 
Poison?can be procured by?a blood relative 
Mr. Mason?visited?at 3:00 P.M.

I would change "relation" to "verb" and use fgets() to put each line of the file, into a char buffer[] array. Then take all the char's from the first up to the first question mark, and copy that into your subject member. Then take everything from that point, until the next question mark, as your verb member. The last bunch of char's (which would always end with the end of string char: '\0'), go into your object member.

Scanf() isn't a good function to use because each of these groups contain a variable number of spaces, which will be a witch for scanf() to work with.

Include string.h, and use that question mark - that's the key thing.

When you're done with one line, you're ready for the next line. You guessed it, this goes in an outer loop. fgets() returns NULL when it has reached the end of the file, so it works great right inside a while loop:

char buffer[85] = {'\0'};

while((fgets(buffer, sizeof(buffer), FilePointer)) != NULL) {

//rest of your code goes here for the handling of the file data

}

I must store the information in an array, so I can search the array later,not the text file.

Should I use array [ARR_SIZE][ARR_SIZE][ARR_SIZE] and use strcpy? or make a seaparate one for each member?

You should also include a null terminator in your array, is good practice.

int readFile(char fileName[], char textStr[])
{
	FILE *fptr;
	char c;
	int i = 0;
	
	if ((fptr = fopen(fileName, "r")) == NULL){
		printf("Error: failed to open %s\n", fileName);
		return -1;
	}
	
	while ((c = fgetc(fptr)) != EOF) {
	    if (i == MAX_FILE - 1)
	        break;
		textStr[i++] = c;
    }
		
	textStr[i] = '\0';
	
	return i;	
}

Well, now I am getting somewhere!

The Array contains:

Lord Dunsmore?is married to?Lady Emily
Miss Secre

Just How do I take advantage of those "?"'s and put the first part in subject, the second part in relation and the third in object?

I am thinking "Tokenizer" but I have never used it before.

Loop the array and check for that char value '?'. When you find that value you can take whatever you had before and use it whichever way you want.

I don't want to give you the code cause you won't learn like that, but you can look at it this way.

1. Lord Dunsmore? is on the array
2. For loop to go through each character
3. Store each value on a new variable until you find the "?" character, then you stop.

That's the easiest way I could possibly explain this. Hope it helps

You have an array of structs with members. Why not just copy them char by char, into the appropriate member? You will use two pointers - one to tell you where the current char is in the buffer array, and one to tell you where that char goes, in the struct member.

I suggest using a while loop to do this, nested inside the while loop that fgets() the strings from the file. Might even use three of them:

j=0;
while((fgets()) {  //as I showed in my earlier post
  i=k=0;
  while(buffer[i] != '?') {
    struct[j].subject[k] = buffer[i];
  i=k=0;
  //repeat the above while for verb and object
  //resesting i and k to 0, but not j, for each of these
  //three inner while loops
  }
}

If you break this problem down into it's three parts, then the whole problem becomes very simple. That's the key solving this problem - and a great skill to develop, for a programmer.

Hello again,
I am sorry to be so stupid but I cn not figure out how to get the next line into the array. I tired making another loop but all I end up with is the first one which is perfect and then the same paragraph 49 times that contain "Lord Dunsmore is married to Lady Emily"

char fillArray(){
FILE *fptr;	
char c;
char textStr[50];
int i = 0; 
int k = 0;	
if ((fptr = fopen("clue.dat", "r")) == NULL){		
printf("Error: failed to open %s\n", "clue.dat");		
return -1;	
} 	
while ((c = fgetc(fptr)) != EOF && c != '\n' ) {	    
if (i == 50 - 1)	        
break;		
textStr[i++] = c; 
} 	
textStr[i] = '\0';
FACTS afact;
  FACTS array[50];   
  printf("\n\nThe Array contains: \n\n");
  for(k=0;k<50; k++){
    
    for (i=0; i <50&& textStr[i]!='?' ; i++){ 
    strcpy(array[k].factArray,&textStr[i]);
    printf("%s",array[i].factArray);
}
    printf("\n");
    
}
      
}

[output]

The Array contains:

Lord Dunsmore?is married to?Lady Emily
e?is married to?Lady Emily ord Dunsmore?is married to?Lady Emily
e?is married to?Lady Emily e?is married to?Lady Emily rd Dunsmore?is marrie
d to?Lady Emily
e?is married to?Lady Emily e?is married to?Lady Emily e?is married to?Lady
Emily d Dunsmore?is married to?Lady Emily
e?is married to?Lady Emily e?is married to?Lady Emily e?is married to?Lady
Emily e?is married to?Lady Emily Dunsmore?is married to?Lady Emily
e?is married to?Lady Emily e?is married to?Lady Emily e?is married to?Lady
Emily e?is married to?Lady Emily e?is married to?Lady Emily Dunsmore?is mar
ried to?Lady Emily
e?is married to?Lady Emily e?is married to?Lady Emily e?is married to?Lady
Emily e?is married to?Lady Emily e?is married to?Lady Emily e?is married to
?Lady Emily unsmore?is married to?Lady Emily
e?is married to?Lady Emily e?is married to?Lady Emily e?is married to?Lady
Emily e?is married to?Lady Emily e?is married to?Lady Emily e?is married to
?Lady Emily e?is married to?Lady Emily nsmore?is married to?Lady Emily
e?is married to?Lady Emily e?is married to?Lady Emily e?is married to?Lady
Emily e?is married to?Lady Emily e?is married to?Lady Emily e?is married to
?Lady Emily e?is married to?Lady Emily e?is married to?Lady Emily smore?is
married to?Lady Emily
e?is married to?Lady Emily e?is married to?Lady Emily e?is married to?Lady
[/output]

@ADAK
I truly appreciate the help, but...[SELF PITY MOMENT] Maybe they are right and I am too old to relearn this!I only have 5 months to do it[/SELF PITY MOMENT] anywho...

I must have made a mistake, the program freezes if I try to do this way.

char fillArray(){
//FILE *fptr;	
//char c;
//char textStr[50];
//int i = 0; 
//int k = 0;	
//if ((fptr = fopen("clue.dat", "r")) == NULL){		
//printf("Error: failed to open %s\n", "clue.dat");		
//return -1;	
//} 	
//while ((c = fgetc(fptr)) != EOF && c != '\n' ) {	    
//if (i == 50 - 1)	        
//break;		
//textStr[i++] = c; 
//} 	
//textStr[i] = '\0';
//FACTS afact;
//  FACTS array[50];   
//  printf("\n\nThe Array contains: \n\n");
//  for(k=0;k<50; k++){
//    
//    for (i=0; i <50&& textStr[i]!='?' ; i++){ 
//    strcpy(array[k].factArray,&textStr[i]);
//    printf("%s",array[i].factArray);
//}
//    printf("\n");
//    
//}
  FILE *cFile;     
  int i,j,k;     
  char *array[ARR_SIZE]; 
  FACTS array1[ARR_SIZE];   
  char c; 
  char buffer[50] = {'\0'};      
  cFile = fopen ("Clue.dat","r"); /*Reopen the file input*/       
  printf("\n\nThe Array contains: \n\n"); 
  j=0;
 
  while((fgets(buffer, sizeof(buffer), cFile)) != NULL) {
   i=k=0;
   for (i=0; i <ARR_SIZE; i++){ 
       for (k=0; k <ARR_SIZE; k++){ 
  
       while(buffer[i] != '?') {
        array1[j].subject[k] = buffer[i];
}
}
}
printf("%s",array1);
      i=k=0;
  for (i=0; i <ARR_SIZE; i++){ 
       for (k=0; k <ARR_SIZE; k++){ 
  
       while(buffer[i] != '?') {
        array1[j].relation[k] = buffer[i];
}
}
}
  printf("%s",array1);
      i=k=0;
  for (i=0; i <ARR_SIZE; i++){ 
       for (k=0; k <ARR_SIZE; k++){ 
  
       while(buffer[i] != '?') {
        array1[j].object[k] = buffer[i];
}
}
}
}

}

WHY the heck can't I just get a string from the text file and park it in an element of an array!! All this char foo[] stuff is giving me a major pain in the brain.

I want to take each whole line from the text file and put it into an element of an array, but I can not seem to figure it out! I have tried everyones suggestions but they just don't seem to work for me. Can too much C cause brain damage, because I just can't think anymore.

this is what Ihave come up with, it reads the first line and then copies it to the fact array. I can't seem to get it to go any further than the first line without it printing a mess

void fillArray(){
    FILE *fp;
     char line[50];
      int max;
     
fp = fopen("clue.dat", "r"); 

int nch = 0;
char c;
char string[50];
int x=0;
max = max - 1;		
	char factArray [7];
printf("\n\n");
 do{
while((c = fgetc(fp)) != EOF){
	
	if(nch < max)
		line[nch++] = c;
			factArray[x] = c;
		printf("%c",factArray[x]);
		x++;
    }
    }while (c == '\n');

if(c == EOF && nch == 0)
	fclose(fp);

line[nch] = '\0';

}

Hello, New Day..Bright and Sunny again, me that is! I worked it out while I was sleeping. It is bad to run code in your head while you sleep! LOL
Anywho...
I borrowed some from ADAk and some from Mr.Diaz and got it working, YAY!

Thank-you both for your help!!!

Until you start dreaming once in a while, about coding, you're not really coding! :)

Congrats on getting it to work - I'm sure it was a real learning experience. Working with strings in C is not that easy - kind of an "acquired taste", really.

Anyway, congrats again and well done. This kind of input, where the number of words varies from one line to the next, can be a real challenge for beginners.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.