【发布时间】:2017-10-15 04:54:32
【问题描述】:
我的方法是从文件中读取每个字符并保持计数,因此当我们遇到非法字符时,我会跟踪字符串长度以及遇到此长度的字符串的计数。现在我正在尝试使用我读入的字符构建字符串并将它们存储在一个数组中。它几乎可以工作,但是当我尝试将 2 个字符串加在一起时,我可以绕过中止和段错误,以防读入的 2 个字符串长度相同。如果您不介意给我一些反馈,我在代码的第 129 行标记了我遇到问题的地方......我希望在完成后打印每个长度的字符串
这是我用来测试的文本文件:
Tomorrow, and tomorrow, and tomorrow,
To the last syllable of recorded time;
源代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
*this program reads in a text file from the command line
*then counts and stores the number of words of all lengths
*/
#define LENGTH 34
#define WORD_BUFFER 750
int strLengths[LENGTH],lengthsCopy[LENGTH];
char *array[WORD_BUFFER][LENGTH];
char strings[LENGTH];
int counter = 0;
int ch,tester;
//sorts the output of string lengths printing the largest amounts first
void sort()
{
int max_val =0;
int i,j,temp,val;
//create copy
for (i=0; i < LENGTH; i++)
{
lengthsCopy[i] = strLengths[i];
}
//for loop finds the max value in the array elements
for(i=0; i<LENGTH; i++)
{
if(lengthsCopy[i] > max_val)
max_val = lengthsCopy[i];
}
printf("max val in the array is %d\n",max_val);
//prints the max value,decrements,checks,prints, rinse repeat...
//iterates until the max is 0
while(max_val!=0)
{
//checks all elements
for(i=LENGTH-1; i > 0; i--)
{
//print when max val is found
if(lengthsCopy[i] == max_val)
{
temp = i;
printf("Count[%02d]=%02d;\n",i,max_val);
//check for doubles
for(j=LENGTH-1; j > 0; j--)
{
//if double is found that is not the original, print
if(lengthsCopy[j] == max_val && temp != j)
{
printf("Count[%02d]=%02d;\n",j,max_val);
//erase value
lengthsCopy[j] = 0;
}
}
}
}
max_val--;
}
}
//print all array that are not null, represent count of word lenghts
void printList()
{
int i,val;
for(i=1; i<=LENGTH;i++)
{
if(strLengths[i] > 0)
{
val = strLengths[i];
printf("Count[%02d]=%02d;\n",i,val);
}
}
}
int main (int argc, char *argv[])
{
//error message if input file is not passed
if(argc < 2)
{
printf("You have to give me a file!\n");
exit(1);
}
FILE *text = fopen(argv[1], "r");
//errror message if no contents in the file
if(text == NULL)
{
printf("No content to read in %s. \n", argv[1]);
exit(1);
}
//iterate through text until end of file
ch = fgetc(text);
int strPoint =0;
while(ch != EOF)
{
//if illegal char is met, add a count to the array value of current counter
//set counter back to 0
//scan next char
if(ch==' '||ch==','||ch=='('||ch==')'||ch==';'||ch=='\n')
{
if(array[counter][0] == NULL)//if length not defined yet
{
array[counter][0] = strings;//add current string build to the array
printf("%s\n",array[counter][0] );
}
else if(array[counter][0] != NULL && strings[0] != '\0')
{//else length is defined add to text bank
printf("else if reached\n");
printf("%s\n",strings );
printf("%lu\n",strlen(array[counter][0]) );
int arrayptr = strlen(*array[counter]);
printf("ptr %d",arrayptr);
/* next line aborts / seg_faults */
strncat(*array[counter],strings,strlen(strings));
}
strLengths[counter]++;
counter = 0;
ch = fgetc(text);
memset(strings, 0, sizeof(strings));//clear stringBuild
strPoint =0;
}
//else a legal character, increase counter, scan next char
else
{
strings[strPoint] = ch;
printf("string build %c\n",strings[strPoint]);
counter++;
strPoint++;
ch = fgetc(text);
}
}
fclose(text);
printf("stored string %s\n",array[3][0] );
printList();
//call sort
sort();
exit(0);
}
【问题讨论】:
标签: c arrays string segmentation-fault abort