c-循环遍历结构和数组时打印的值不正确



我正在从一个文件中读取,并将每一行的值保存到一个结构中。我保存的值是一个字符、一个字符串和两个整数。然后将每个结构保存到一个结构数组中。我想打印出这些值中的每一个,以确保它们被正确保存,并且当我从for循环中的数组访问每个结构时,除了字符串之外的所有内容都会正确打印出来。我不明白为什么会发生这种事。为了进行检查,我在将字符串添加到给出预期输出的结构中后立即打印出这些字符串。只有当我试图访问for循环中的这些字符串元素时,我才会得到不正确的输出。这是我的代码:

char wordFirstLetter;
struct BankAccount arrAccounts[20];
struct TransactionRequest arrTransactions[20];
int numAccounts = 0;
int numTransactions = 0;
int currAccountIndex = 0;
int currClient = 1;
while(fgets(line, sizeof(line),fp))
{
// We will be getting the words on each line using strtok()
//Gets the first word in the line
char *word = strtok(line, " ");
wordFirstLetter = word[0];
// Checks if the first letter of the line is 'a'
// If it is, then we know we are setting up the account
if(wordFirstLetter == 'a')
{
//not related
}
// Otherwise we are dealing with a client
else
{
while(word != NULL)
{
if(word[0] == 'c')
{
// Move on to the next word if we see that we are
// dealing with a client
word = strtok(NULL, " ");
}
else
{
// Create a structure to represent the current request
struct TransactionRequest transaction;
// Append the client number of the client doing the transaction
transaction.client = currClient;

// Append the type of transaction and move to next word
char *transType = word;
transaction.requestType = transType;
printf("This is the value of word: %sn", word);
printf("%sn", transaction.requestType);
word = strtok(NULL, " ");

// Append the account number that will be altered and move to next word
transaction.account = word[1] - '0';
word = strtok(NULL, " ");

// Append the amount for the transaction and move to next word
int transAmount = atoi(word);
transaction.amount = transAmount;
word = strtok(NULL, " ");

// Append this transaction to an array containing all transactions in the file
arrTransactions[numTransactions] = transaction;
numTransactions++;
}
}
// Each line represents a client, so when we are done with the
// line we move to the next client
currClient++;
}
}
for(int i = 0; i < numTransactions; i++)
{
struct TransactionRequest transaction = arrTransactions[i];
printf("This is the client number: %dn", transaction.client);
printf("This is the request type: %sn", transaction.requestType);
printf("This is the account to be accessed: %dn", transaction.account);
printf("This is the amount: %dn", transaction.amount);
printf("n");
}

这是我正在使用的结构:

struct TransactionRequest
{
// ID if the client doing the transaction
int client;
// Type of transaction the client is doing
char *requestType;
// Account number of the account the client is dealing with
int account;
// Amount of money that is boing moved around
int amount;
};

当打印出这些字符串元素时,我是否在for循环中做了错误的操作?如果没有,为什么会出现这个问题。

wordwhile循环后超出范围,因此您的结构将包含指向无效数据的指针。即使在while循环之前声明了wordTransactionRequest的所有实例都将包含word的最新值,这可能不是预期的行为。如果您知道requestType不会超过某个长度,那么您可以在结构中使用固定长度的char数组。这样,无论您的结构实例是在堆栈还是堆上分配的,requestType数据对每个实例都是唯一的,并且将与整个结构一起分配/释放。

#define REQUEST_TYPE_MAX_LENGTH 256
struct TransactionRequest
{
// ID if the client doing the transaction
int client;
// Type of transaction the client is doing
char requestType[REQUEST_TYPE_MAX_LENGTH];
// Account number of the account the client is dealing with
int account;
// Amount of money that is boing moved around
int amount;
};

尽管如此,仍需要对代码进行重大的重新设计。例如,不能使用赋值将数据从word变量传输到TranscationRequest结构的requestType字段。您将需要使用诸如strncat之类的带有手动空终止的功能。

虽然它不是严格相关的,但我也想指出的是,TransactionRequest中的帐号只能具有0到9(包括0和9(之间的值,除非您的原始文件包含一些奇怪的ASCII字符。这是有意的吗?

最新更新