实验室中0x6BE20E11 (ucrtbased.dll) 的未处理异常.exe: 0xC0000005:访问冲突读取

Unhandled exception at 0x6BE20E11 (ucrtbased.dll) in labs.exe: 0xC0000005: Access violation reading location 0x0000004D

本文关键字:exe 异常 0xC0000005 读取 访问冲突 未处理 0x6BE20E11 ucrtbased dll 实验室      更新时间:2023-10-16

看起来我的函数makeUpper中存在错误。我尝试了指针和其他一些方法,但没有用。以下是排序中的内容.txt :

Michigan
Montana
New York
Alabama
WYOMING
South Carolina
mISSISSIPPI
Iowa
ohio

我的目标是将所有字母大写并按字母顺序列出。请帮助我。

#include <stdio.h>
#include <string.h>
#include <ctype.h>
void bubblesort(char line[][30], int n);
void swapStrings(char *first[], char *second[]);
void makeUpper(char first[][30], char upperFirst[][30]);
int main()
{
    char state[NUM][30];
    char upper[NUM][30];
    int i, nspot;
    FILE* infile;
    /*Read file into first array.*/
    infile = fopen("sorting.txt", "r");
    if (infile == 0)
    {
        printf("trouble opening file.n");
        return(0);
    }
    i = 0;
    while ((fgets(state[i], 30, infile)) != NULL)
    {
        nspot = strlen(state[i]) - 1;
        if (state[i][nspot] == 'n')
            state[i][nspot] = ''; 
        i++;
    }
    makeUpper(state, upper);
    bubblesort(upper, NUM);
    for (i = 0; i < NUM; i++)
    {
        printf("%sn", upper[i]);
    }
    fclose(infile);
    return 0;
}

void bubblesort(char line[][30], int n)
{
    int last;
    int i;
    for (last = n - 1; last >= 1; last--)
        for (i = 0; i <= last - 1; i++)
        {
            if (strcmp(line[i], line[i + 1]) > 0)
            swapStrings(&line[i], &line[i + 1]);
        }
}
void swapStrings(char* first[], char* second[])
{
    char swap[30];
    strcpy(swap, first);
    strcpy(first, second);
    strcpy(second, swap);
}
void makeUpper(char first[][30], char upperFirst[][30])
{
    int i,j;
    int test;
    for (i = 0; i < NUM; i++)
    {
        for (j = 0; j < (strlen(first[i])); j++)
        {
            if (first[i][j] > 90)
                strcpy(upperFirst[i][j], toupper(first[i][j]));
            else
                strcpy(upperFirst[i][j], first[i][j]);
        }
        upperFirst[i][strlen(first[i])] = '';
    }
}

取而代之的是:

strcpy(upperFirst[i][j], toupper(first[i][j]));

你需要这个:

upperFirst[i][j] = toupper(first[i][j]);

相关文章: