C-对文件中的字符串进行排序,但内存使用有限

C-对文件中的字符串进行排序,但内存使用有限,c,file,sorting,C,File,Sorting,我正在编写一个程序,它生成字符数组并将它们添加到文件中,所以它就像一个字符数组。生成零件很好,文件也已正确创建和保存。 然而,其中一项任务说我应该使用插入排序(也使用librabry和系统选项)对文件中的记录进行排序。排序的关键是记录的第一个字节的值。而且,我的内存中一次只能有两条记录 到目前为止,我已经得到了以下代码:生成大小为bufferSize的记录的recordsAmount void generate(char *path, int recordsAmount, int bufferS

我正在编写一个程序,它生成字符数组并将它们添加到文件中,所以它就像一个字符数组。生成零件很好,文件也已正确创建和保存。 然而,其中一项任务说我应该使用插入排序(也使用librabry和系统选项)对文件中的记录进行排序。排序的关键是记录的第一个字节的值。而且,我的内存中一次只能有两条记录

到目前为止,我已经得到了以下代码:生成大小为
bufferSize
的记录的
recordsAmount

void generate(char *path, int recordsAmount, int bufferSize)
{

    int i,j;

    int start = open(path,O_CREAT| O_TRUNC | O_WRONLY | S_IRUSR | S_IWUSR);
    for(i = 0; i < recordsAmount; i++){
             char buffer[bufferSize];
        for(j = 0; j < bufferSize; j++){
            buffer[j] = 'A' + (rand() % 26);
            write(start, &buffer[j], sizeof(buffer[j]));

        }

    write(start, "\n", 1);
    }

}
图书馆:

==804== Memcheck, a memory error detector
==804== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==804== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==804== Command: ./program2 sort ZZZ 10 10 lib
==804==
==804== Invalid read of size 4
==804==    at 0x48E24A7: fseek (fseek.c:35)
==804==    by 0x10A014: libSort (in /mnt/c/Users/Czaro/ubuntu/program2)
==804==    by 0x1095F6: main (in /mnt/c/Users/Czaro/ubuntu/program2)
==804==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==804==
==804==
==804== Process terminating with default action of signal 11 (SIGSEGV)
==804==  Access not within mapped region at address 0x0
==804==    at 0x48E24A7: fseek (fseek.c:35)
==804==    by 0x10A014: libSort (in /mnt/c/Users/Czaro/ubuntu/program2)
==804==    by 0x1095F6: main (in /mnt/c/Users/Czaro/ubuntu/program2)
==804==  If you believe this happened as a result of a stack
==804==  overflow in your program's main thread (unlikely but
==804==  possible), you can try to increase the size of the
==804==  main thread stack using the --main-stacksize= flag.
==804==  The main thread stack size used in this run was 8388608.
==804==
==804== HEAP SUMMARY:
==804==     in use at exit: 0 bytes in 0 blocks
==804==   total heap usage: 1 allocs, 1 frees, 472 bytes allocated
==804==
==804== All heap blocks were freed -- no leaks are possible
==804==
==804== For lists of detected and suppressed errors, rerun with: -s
==804== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault

sysSort()
libSort()
中的问题似乎都是它们没有成功打开文件
open()
在此事件中返回-1(不是
NULL
),它与您在这种情况下得到的所有警告相匹配。由于
sysSort()
无法打开文件,因此
libSort()
似乎也无法打开文件,但该函数不会测试打开是否成功。相反,如果失败,
libSort()
使用生成的空指针会产生未定义的行为,segfault是一种完全合理的表现形式

我猜根本原因是创建文件时没有正确设置文件的权限。这可能是由于在
generate()
中调用了错误的
open()

在打开标志中包含
O_create
时,必须传递一个附加参数,指定创建新文件时要使用的初始模式。而是
将模式位插入标志,这是不正确的。这将解决该特定问题,我怀疑这将允许两个排序函数随后成功打开该文件:

    int start = open(path, O_CREAT| O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR);

您可以通过valgrind运行该文件以检查它在哪里做了不应该做的事情。我做了,粘贴了我在帖子中得到的内容,以前从未使用过valgrind…
if((file=open(path,O_RDWR))==NULL){
您在这里混淆了文件指针和文件描述符。
open()
返回一个int,而不是指针。另外,
sysSort()
打开文件两次(或至少尝试打开),然后将其关闭零次。这将在每次调用中泄漏最多两个打开的文件描述。另一方面,
libSort()
不会检查打开文件是否成功。
sysSort()
结果表明打开不会成功,这就解释了为什么在
libSort()
中会出现segfault(变量
文件
NULL
,我想您会发现)。我将标志更改为O|CREAT | O|TRUNC | O|RDWR,但我没有做任何事情。现在函数打开一个文件,但是,当我有5行5个字母时,
libSort
用一条记录再生成100行,
sysSort
不会生成任何输出我很抱歉,您的程序还没有正常工作,@krysznys,但显然没有您询问的问题已解决,因为
libSort()
现在可以修改该文件。感谢帮助,我将其标记为已批准的答案!:D
==803== Memcheck, a memory error detector
==803== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==803== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==803== Command: ./program2 sort ZZZ 10 10 sys
==803==
==803== Warning: invalid file descriptor -1 in syscall read()
==803== Warning: invalid file descriptor -1 in syscall read()
==803== Conditional jump or move depends on uninitialised value(s)
==803==    at 0x109DB6: sysSort (in /mnt/c/Users/Czaro/ubuntu/program2)
==803==    by 0x1095A7: main (in /mnt/c/Users/Czaro/ubuntu/program2)
==803==
==803== Warning: invalid file descriptor -1 in syscall write()
==803== Warning: invalid file descriptor -1 in syscall read()
==803== Warning: invalid file descriptor -1 in syscall read()
==803== Warning: invalid file descriptor -1 in syscall write()
==803== Warning: invalid file descriptor -1 in syscall read()
==803== Warning: invalid file descriptor -1 in syscall read()
==803== Warning: invalid file descriptor -1 in syscall write()
==803== Warning: invalid file descriptor -1 in syscall read()
==803== Warning: invalid file descriptor -1 in syscall read()
==803== Warning: invalid file descriptor -1 in syscall write()
==803== Warning: invalid file descriptor -1 in syscall read()
==803== Warning: invalid file descriptor -1 in syscall read()
==803== Warning: invalid file descriptor -1 in syscall write()
==803== Warning: invalid file descriptor -1 in syscall read()
==803== Warning: invalid file descriptor -1 in syscall read()
==803== Warning: invalid file descriptor -1 in syscall write()
==803== Warning: invalid file descriptor -1 in syscall read()
==803== Warning: invalid file descriptor -1 in syscall read()
==803== Warning: invalid file descriptor -1 in syscall write()
==803== Warning: invalid file descriptor -1 in syscall read()
==803== Warning: invalid file descriptor -1 in syscall read()
==803== Warning: invalid file descriptor -1 in syscall write()
==803== Warning: invalid file descriptor -1 in syscall read()
==803== Warning: invalid file descriptor -1 in syscall read()
==803== Warning: invalid file descriptor -1 in syscall write()
==803==
==803== HEAP SUMMARY:
==803==     in use at exit: 0 bytes in 0 blocks
==803==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==803==
==803== All heap blocks were freed -- no leaks are possible
==803==
==803== Use --track-origins=yes to see where uninitialised values come from
==803== For lists of detected and suppressed errors, rerun with: -s
==803== ERROR SUMMARY: 9 errors from 1 contexts (suppressed: 0 from 0)
==804== Memcheck, a memory error detector
==804== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==804== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==804== Command: ./program2 sort ZZZ 10 10 lib
==804==
==804== Invalid read of size 4
==804==    at 0x48E24A7: fseek (fseek.c:35)
==804==    by 0x10A014: libSort (in /mnt/c/Users/Czaro/ubuntu/program2)
==804==    by 0x1095F6: main (in /mnt/c/Users/Czaro/ubuntu/program2)
==804==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==804==
==804==
==804== Process terminating with default action of signal 11 (SIGSEGV)
==804==  Access not within mapped region at address 0x0
==804==    at 0x48E24A7: fseek (fseek.c:35)
==804==    by 0x10A014: libSort (in /mnt/c/Users/Czaro/ubuntu/program2)
==804==    by 0x1095F6: main (in /mnt/c/Users/Czaro/ubuntu/program2)
==804==  If you believe this happened as a result of a stack
==804==  overflow in your program's main thread (unlikely but
==804==  possible), you can try to increase the size of the
==804==  main thread stack using the --main-stacksize= flag.
==804==  The main thread stack size used in this run was 8388608.
==804==
==804== HEAP SUMMARY:
==804==     in use at exit: 0 bytes in 0 blocks
==804==   total heap usage: 1 allocs, 1 frees, 472 bytes allocated
==804==
==804== All heap blocks were freed -- no leaks are possible
==804==
==804== For lists of detected and suppressed errors, rerun with: -s
==804== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault
    int start = open(path,O_CREAT| O_TRUNC | O_WRONLY | S_IRUSR | S_IWUSR);
    int start = open(path, O_CREAT| O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR);