C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 7th, 2002, 07:49 AM
gareh's Avatar
gareh gareh is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Liverpool, UK
Posts: 0 gareh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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) ;
   }

}

Reply With Quote
  #2  
Old August 7th, 2002, 08:51 AM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed God (5000 - 5499 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 5,163 Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 6 Days 1 h 34 m 20 sec
Reputation Power: 791
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;
}

Reply With Quote
  #3  
Old August 7th, 2002, 09:08 AM
gareh's Avatar
gareh gareh is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Liverpool, UK
Posts: 0 gareh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks. I'll give that a go.

Rob

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Strings and Arrays... Could someone please help?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap