So I have a file containing lets say:
所以我有一个文件包含让我们说:
cat
dog
cat
I am trying to go through the file, have it recognize there are two cat
elements and one dog
element, and then have in the same file edited as:
我正在尝试浏览文件,让它识别出有两个cat元素和一个dog元素,然后在同一个文件中编辑为:
cat - 2
dog - 1
I already have all the words saved in an array of strings, char **wordList
, and I am trying to sort them with qsort and then put it in the format as described above. My qsort functions are:
我已经将所有单词保存在一个字符串数组中,char ** wordList,我试图用qsort对它们进行排序,然后将其放入如上所述的格式中。我的qsort功能是:
stringcmp(const void *a, const void *b)
{
const char **ia = (const char **)a;
const char **ib = (const char **)b;
return strcmp(*ia, *ib);
}
void wordSort(char **wordlist)
{
size_t strings_len = numwords - 1;
qsort(wordlist, strings_len, sizeof(char*), stringcmp);
wordFile(wordlist);
}
void wordFile(char **wordlist)
{
if((outFilePtr2 = fopen(outWords, "w")) != NULL)
{
for(x = 1; x < numwords; x++)
{
fputs(wordlist[x], outFilePtr2);
fputs("\n", outFilePtr2);
}
fclose(outFilePtr2);
}
else
{
printf("File\"%s\" could not be opened.\n", outWords);
}
}
It is not sorting anything in order though. How do I fix it?
虽然它没有按顺序排序。我如何解决它?
1 个解决方案
#1
2
The following program works with your definition of stringcmp (which seems correct):
以下程序适用于您对stringcmp的定义(看起来是正确的):
int main (int argc, char *argv[]) {
int i;
qsort(argv, argc, sizeof(char *), &stringcmp);
for (i = 0; i != argc; i++) printf("%s\n", argv[i]);
}
Thus I suspect that you have a problem with the definition of char **wordList
.
因此我怀疑你对char ** wordList的定义有问题。
UPDATE
UPDATE
This version slightly modified/completed version of your program works for me:
这个版本稍微修改/完成的程序版本适合我:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *outWords = "outWords.txt";
char *wordList[] = { "cat", "dog", "cat" };
#define numwords (sizeof(wordList) / sizeof(wordList[0]))
FILE *outFilePtr2;
int x;
int stringcmp(const void *a, const void *b)
{
const char **ia = (const char **)a;
const char **ib = (const char **)b;
return strcmp(*ia, *ib);
}
void wordSort(char **wordlist)
{
qsort(wordlist, numwords, sizeof(char*), stringcmp);
wordFile(wordlist);
}
void wordFile(char **wordlist)
{
if((outFilePtr2 = fopen(outWords, "w")) != NULL)
{
for(x = 0; x < numwords; x++)
{
fputs(wordlist[x], outFilePtr2);
fputs("\n", outFilePtr2);
}
fclose(outFilePtr2);
}
else
{
printf("File\"%s\" could not be opened.\n", outWords);
}
}
int main() {
wordSort(wordList);
wordFile(wordList);
return 0;
}
I adapted the second argument of qsort
(else the last string pointer would not be considered, and left unchanged). I also adapted the initialization x=0
of the for
-loop in wordFile
for the first string to be printed, too.
我改编了qsort的第二个参数(否则最后一个字符串指针将不被考虑,并保持不变)。我还在wordFile中修改了for循环的初始化x = 0,以便打印第一个字符串。
You may have defined **wordList in some other way causing a problem, you did not provide the code for it.
您可能以某种其他方式定义了** wordList导致问题,您没有为它提供代码。
#1
2
The following program works with your definition of stringcmp (which seems correct):
以下程序适用于您对stringcmp的定义(看起来是正确的):
int main (int argc, char *argv[]) {
int i;
qsort(argv, argc, sizeof(char *), &stringcmp);
for (i = 0; i != argc; i++) printf("%s\n", argv[i]);
}
Thus I suspect that you have a problem with the definition of char **wordList
.
因此我怀疑你对char ** wordList的定义有问题。
UPDATE
UPDATE
This version slightly modified/completed version of your program works for me:
这个版本稍微修改/完成的程序版本适合我:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *outWords = "outWords.txt";
char *wordList[] = { "cat", "dog", "cat" };
#define numwords (sizeof(wordList) / sizeof(wordList[0]))
FILE *outFilePtr2;
int x;
int stringcmp(const void *a, const void *b)
{
const char **ia = (const char **)a;
const char **ib = (const char **)b;
return strcmp(*ia, *ib);
}
void wordSort(char **wordlist)
{
qsort(wordlist, numwords, sizeof(char*), stringcmp);
wordFile(wordlist);
}
void wordFile(char **wordlist)
{
if((outFilePtr2 = fopen(outWords, "w")) != NULL)
{
for(x = 0; x < numwords; x++)
{
fputs(wordlist[x], outFilePtr2);
fputs("\n", outFilePtr2);
}
fclose(outFilePtr2);
}
else
{
printf("File\"%s\" could not be opened.\n", outWords);
}
}
int main() {
wordSort(wordList);
wordFile(wordList);
return 0;
}
I adapted the second argument of qsort
(else the last string pointer would not be considered, and left unchanged). I also adapted the initialization x=0
of the for
-loop in wordFile
for the first string to be printed, too.
我改编了qsort的第二个参数(否则最后一个字符串指针将不被考虑,并保持不变)。我还在wordFile中修改了for循环的初始化x = 0,以便打印第一个字符串。
You may have defined **wordList in some other way causing a problem, you did not provide the code for it.
您可能以某种其他方式定义了** wordList导致问题,您没有为它提供代码。