[英]Unhandled exception at 0x5E10F1C0 (ucrtbased.dll) in HW 5 Rational pt2.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC. occurred
[英]Unhandled exception at 0x6BE20E11 (ucrtbased.dll) in labs.exe: 0xC0000005: Access violation reading location 0x0000004D
看起來我的函數makeUpper中有bug。 我已經嘗試過指針和其他一些方法,但它沒有用。 以下是sorting.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] = '\0';
i++;
}
makeUpper(state, upper);
bubblesort(upper, NUM);
for (i = 0; i < NUM; i++)
{
printf("%s\n", 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])] = '\0';
}
}
而不是這個:
strcpy(upperFirst[i][j], toupper(first[i][j]));
你需要這個:
upperFirst[i][j] = toupper(first[i][j]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.