I am wondering how i can set a struct's string value with a tokenized line from a file. basically i am reading in a line like "Person 100 100"
(delimited by \\t
) and i need to set the string value of a struct with what's returned.
Error message:
||In function 'main':|
|32|warning: passing argument 1 of 'strcpy' from incompatible pointer type|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\string.h|45|note: expected 'char *' but argument is of type 'char **'|
||=== Build finished: 0 errors, 1 warnings ===|
Code snippet:
char buffer[20];
fgets(buffer, 20, file);
while (*buffer != EOF)
{
struct student temp;
char *result = NULL;
//set name
strcpy(temp.name,strtok(buffer,"\t"));
//set midterm
result = strtok(NULL, "\t");
temp.midterm = atoi(result);
//set final
result = strtok(NULL, "\t");
temp.final = atoi(result);
}
strcpy function is define as follows:
char *strcpy(char *restrict s1, const char *restrict s2);
do not know the student structure, so you may pass parameters error in the first parameters. and below code is ok:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
char name[128];
int midterm;
int final;
};
char buffer[] = {"Person 100 100"};
//fgets(buffer, 20, file);
int main()
{
//while (*buffer != EOF)
{
struct student temp;
char *result = NULL;
//set name
strcpy(temp.name,strtok(buffer,"\t"));
//set midterm
result = strtok(NULL, "\t");
temp.midterm = atoi(result);
//set final
result = strtok(NULL, "\t");
temp.final = atoi(result);
printf("name = %s, midterm = %d, final = %d\n", temp.name, temp.midterm, temp.final);
}
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.