使用Strtok存储字符串的片断

时间:2013-10-15 04:55:54

标签: c arrays string strtok

#include <stdio.h>
#include <math.h>
#include <string.h>
int main(void)
{
    int menuswitch=1;
    int amountofstudents;
    int fname[50];
    int lname[50];
    int grade[50];
    int i;
    char studentinfo[100];
    printf("Enter Amount of Students: ");
    scanf("%d", &amountofstudents);
    for (i=0;i<amountofstudents;i++)
    {
        gets(studentinfo);
        strcpy(fname[i], strtok(studentinfo, " "));
        strcpy(lname[i], strtok(NULL, " "));
        strcpy(grade[i], strtok(NULL, " "));
    }

好吧需要一点使用strtok。我试图存储输入字符串的片段以便稍后排序。我正在考虑使用strtok来打破字符串然后将每个片段放在相应的数组中。然而,每次我尝试时,我都会在Visual Studios中收到一个错误提示访问冲突。谢谢你提前帮助

错误是

First-chance exception at 0x5120F7B3 (msvcr110d.dll) in Lab 2.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x5120F7B3 (msvcr110d.dll) in Lab 2.exe: 0xC0000005: Access violation reading location 0x00000000.

输入将是

FirstName Lastname 80(Grade)

3 个答案:

答案 0 :(得分:1)

一个主要问题是您尝试复制到整数值而不是字符串。改变 整数数组到字符串数组:

...
char fname[50][100];
char lname[50][100];
char grade[50][100];
...

你也遇到gets函数的问题(除了它被删除,不应该使用),即前一个scanf没有从输入缓冲区中删除换行符所以第一个gets调用会看到这个空的换行符并给你一个空行(你不检查)。

这可以通过告诉scanf通过在"%d"之后的格式字符串中添加空格来放弃尾随空格来解决这个问题:

scanf("%d ", &amountofstudents);
/*       ^    */
/*       |    */
/* Note space */

而不是gets,您应该使用fgets

fgets(studentinfo, sizeof(studentinfo), stdin);

最后,始终检查错误!

答案 1 :(得分:0)

潜在的问题是scanf/gets combo。使用fgets()代替atoi()并使用char* token = strtok(studentinfo, " "); if ( strlen(token) < sizeof(fname[i]) ) { strcpy(fname[i], token); ... 在适当时转换为整数也可以对strtok 返回的内容进行完整性检查(假设输入任何内容都不好)

char fname[50];

您还将字符串声明为整数数组,它们应该是char 例如{{1}}

答案 2 :(得分:0)

你遇到的问题是你已经将三个变量(fname,lname和grade)声明为char [](数组)(嗯,这是你打算使用的类型),但是你要提示并保留围绕着一堆学生的信息。然后你尝试从strtok()复制到你想成为char []的东西,但是由于你取消引用fname [i](lname [i],grade [i]),它们的类型为char,而不是char []。

你需要stdlib.h来退出,

#include <stdio.h>
#include <stdlib.h> //for exit
#include <string.h>
//#include <math.h> //you don't need this, yet
#define STUDLIMIT (100)

你可以创建一个fname [],lname [],grade []的数组,(见这里:http://www.cs.swarthmore.edu/~newhall/unixhelp/C_arrays.html),

int main(void)
{
    //int menuswitch=1; //you aren't using this
    int amountofstudents;
    char studentinfo[100];
    char fname[STUDLIMIT][50];
    char lname[STUDLIMIT][50];
    char grade[STUDLIMIT][50];
    int ndx;
    printf("Enter Amount of Students: ");
    if( (fscanf(stdin,"%d ", &amountofstudents)<=0)
    || (amountofstudents<1) || (amountofstudents>STUDLIMIT) )
    {
        printf("need %d to %d studends\n",1,STUDLIMIT); exit(0);
    }
    for (ndx=0;ndx<amountofstudents;ndx++)
    {
        printf("Student: "); fflush(stdout);
        fgets(studentinfo,sizeof(studentinfo),stdin);
        strcpy(fname[ndx], strtok(studentinfo, " "));
        strcpy(lname[ndx], strtok(NULL, " "));
        strcpy(grade[ndx], strtok(NULL, " "));
    }
}

或者你可以创建一个struct(ure)来保存输入的学生信息,并实例化这些学生记录的数组,每个学生输入和存储一个,

typedef struct student
{
    char fname[50];
    char lname[50];
    char grade[50];
} StudentObj;
int StudentCopy(StudentObj*sp,char*fname,char*lname,char*grade)
{
    if(!sp || !fname || !lname || !grade ) return -1;
    strcpy(sp->fname, fname);
    strcpy(sp->fname, lname);
    strcpy(sp->fname, grade);
}
StudentObj students[STUDLIMIT];
int main(void)
{
    //int menuswitch=1; //you aren't using this
    int amountofstudents;
    char studentinfo[100];
    char fname[50];
    char lname[50];
    char grade[50];
    int ndx;
    printf("Enter Amount of Students: ");
    if( (fscanf(stdin,"%d ",&amountofstudents)<=0)
    || (amountofstudents<1) || (amountofstudents>STUDLIMIT) )
    {
        printf("need %d to %d studends\n",1,STUDLIMIT); exit(0);
    }
    for (ndx=0;ndx<amountofstudents;ndx++)
    {
        printf("Student: "); fflush(stdout);
        fgets(studentinfo,sizeof(studentinfo),stdin);
        strcpy(fname, strtok(studentinfo, " "));
        strcpy(lname, strtok(NULL, " "));
        strcpy(grade, strtok(NULL, " \n\r"));
        StudentCopy(&(students[ndx]),fname,lname,grade);
    }
}