Reading two words into single char array from text file

I am trying to read in player names (ex: first last) from a text file into the people[].name data struct. I can successfully read in my card file, but I cannot get this to work. I get a seg fault. I believe this is because nothing is actually being read in for my while loops. I cant use std::strings so please dont suggest that these must be c-style strings aka char arrays. all help is apprecaited! thanks

The working card read in is commented out below. it works, and i left it in for explanation on what i am trying to do**

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// deck of cards
// below are initializations
#include <iostream>
#include <fstream>
#include <ctime>
#include <stdlib.h>
#include <string>

using namespace std;

//globals
const int maxCards = 52;

//Structs
struct card {
char suit[8];
char rank[6];
int cvalue;
char location;
};

struct player {
char name[32];
int total;
card hand[4];
};


//program
int main()
{


//constants
char tempfName[100];
char templName[100];

//create struct array(s)
card deck[52];
card shuffledDeck[52];
player people[4];

//create pointers
card * deckPointer =NULL;
deckPointer = deck;
card * shuffledDeckPointer=NULL;
shuffledDeckPointer = shuffledDeck;

//set defalt values
for(int i=0;i<4;i++)
{
    strcopy(people[i].name,"first last");
}
/* ---- this works but is commented out for simplicities sake in fixing error**
//set up card file to be read in
ifstream fin;
string finName;

//get file name from user
cout << "Enter file name...(cardFile.txt)" << endl;;
getline(cin,finName);


//open the file
fin.open(finName.c_str());


//check if cardFile.txt opens correctly
if(!fin.good())
    {
        cout << "Error with card file" << endl;
        return 0;
    }
else
    {
    card *deckPointer = NULL;
    
    //prime fin
    //fin >> deck[i].suit;
    //fin >> deck[i].rank;
    //fin >> deck[i].cvalue;
    while(fin.good())
        {
    for(deckPointer = &deck[0]; deckPointer < &deck[maxCards];deckPointer++)
{
            fin >> (*deckPointer).suit;
        fin >> (*deckPointer).rank;
        fin >> (*deckPointer).cvalue;
}   
        }    
    }            
*/
//open player names file
ifstream fin2;
string fin2Name;

//get file name from user
cout << "Enter player file name...(Players.txt)" << endl;

getline(cin, fin2Name);

//open file
fin2.open(fin2Name.c_str());

//check if Players.txt opens correctly
if(!fin2.good())
    {
        cout << "Error with player file!" << endl;
        return 0;
    }
else
    {

    int j =0;
    fin2 >> people[j].name;  //prime file
    while(fin2.good())
    {
    //find the length
    int index =0, length=0;
        while(tempfName[length] != '\0')
        {
            length++;
        }
    //now add space after first name
    tempfName[length] = ' ';
    length++;
    while(templName[index] != '\0')
    {
        tempfName[length] = templName[index];
        length++;
        index++;
    }
    tempfName[length]='\0';
    int counter =0;
    while(templName[counter] != '\0')
    {
    people[0].name[counter] = templName[counter];
    counter++;
    }
}
        }
Last edited on
assuming both first and last names are on the same line of the file use istream.getline.
http://www.cplusplus.com/reference/istream/istream/getline/
Last edited on
Topic archived. No new replies allowed.