|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
||||
|
||||
|
Strings and Arrays... Could someone please help?
Hi,
I have an input file whos data needs to be entered into a one-dimentional array. The data file is as arranged as shown below. Each line is to be entered into a single array entry as a string. The code I have used so far (which doesn't work) to do this is shown below also. Thanks for any help Rob Code:
DATA FILE --------- 1 S 1 2 S 2 3 S 3 4 S 4 5 S 5 6 S 6 7 S 7 8 S 8 9 S 9 10 S 10 11 S 11 12 S 12 13 S 13 1 H 14 2 H 15 3 H 16 4 H 17 5 H 18 6 H 19 7 H 20 8 H 21 9 H 22 . . . 10 C 49 11 C 50 12 C 51 13 C 52 Code:
C FILE
------
#include <stdio.h>
main()
{
char cardData [7] ;
char fullArray [52] ;
int counter ;
FILE *card_data_file = fopen ("cards.dat", "r") ;
for (counter = 0 ; counter < 52 ; ++ counter)
{
fgets (cardData, 7, card_data_file) ;
strcpy (fullArray [counter], cardData) ;
}
}
|
|
#2
|
||||
|
||||
|
Here is a simple way that you could do it:
Code:
#include <stdio.h>
#include <string.h>
typedef struct _card {
int id;
char suit;
int placement;
} card;
main () {
card deck[52];
int counter=0;
char working[10];
char *temp=&working[0];
char *test;
FILE *card_data_file;
bzero(working,10);
card_data_file = fopen("data.txt","r");
if (card_data_file == NULL) {
printf("Error reading card data file.");
return -1;
}
for(counter=0;counter < 52;counter++) {
fgets(temp, 10, card_data_file);
deck[counter].id=atoi(strtok(temp," "));
test=strtok(NULL," ");
deck[counter].suit=test[0];
deck[counter].placement=atoi(strtok(NULL," "));
}
fclose(card_data_file);
for(counter=0;counter < 52; counter++) {
printf(" Card %d%c is at position %d in the deck\n",
deck[counter].id,
deck[counter].suit,
deck[counter].placement);
}
printf("\nEnd\n");
return 1;
}
|
|
#3
|
||||
|
||||
|
Thanks. I'll give that a go.
Rob |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Strings and Arrays... Could someone please help? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|