相关疑难解决方法(0)

strncpy中的SEGMENTATION FAULT - 来自字典的加载

我有这个函数"加载",我从字典中读取单词并将它们放在链表的哈希表中.当我尝试读取一行并将其保存在我的new_node-> text中时,编译器返回SEGMENTATION FAULT,我不知道为什么.当我使用strncpy时,错误会出现.

#define HASHTABLE_SIZE 76801


typedef struct node
{
        char text[LENGTH+1];
        //char* text;
        //link to the next word
        struct node* next_word;
}
node;


    node* hashtable[HASHTABLE_SIZE];

    bool load(const char* dictionary)
    {
        FILE* file = fopen(dictionary,"r");
        unsigned long index = 0;
        char str[LENGTH+1];

        if(file == NULL)
        {
            printf("Error opening file!");
            return false;
        }

        while(! feof(file))
        {
            node * new_node = malloc(sizeof(node)+1000);


            while( fscanf(file,"%s",str) > 0)
            {
                printf("The word is %s",str);
                strncpy(new_node->text,str,LENGTH+1);
                //strcpy(new_node->text,str);

                new_node->next_word = NULL;
                index = hash( (unsigned char*)new_node->text);

                if(hashtable[index] …
Run Code Online (Sandbox Code Playgroud)

c load segmentation-fault strcpy strncpy

2
推荐指数
1
解决办法
425
查看次数

如何在C++的一行中使用多个整数,而不知道会输入多少?

我正在使用cin >> x[0] >> x[1] >> x[2] >> x[3];等获取输入,例如:

1 2 3 4.

但我的问题是,可能有3个不同的数字(例如,1,2和3)到20个不同的数字,我不知道预先有多少.因为用户最多可以输入20个数字,所以我重复了上面的模式,直到x [19].我发现程序不会继续,直到它为这些值中的每一个都有一个输入.

c++ cin

2
推荐指数
1
解决办法
2850
查看次数

文件末尾有哪些字符?

我对C比较陌生,我的问题是:

是否总是只有EOF字符超过文件末尾?

示例代码:

FILE *fr;
int i;

fr=fopen("file.txt","r");

for (i=0;i<20;i++) {
    putchar(getc(fr));
}

fclose(fr);
Run Code Online (Sandbox Code Playgroud)

输出:

user@host:~$ ./a.out | xxd
0000000: 6173 640a ffff ffff ffff                 asd.......
Run Code Online (Sandbox Code Playgroud)

(file.txt包含asd\n)

c file

2
推荐指数
1
解决办法
151
查看次数

从C中的文件读取的数据开头的大数字

我有一个代码片段,我想从txt文件中读取列式数据.但是,每当我这样做时,一个大数字 - 即 - -1.073742e + 008会被附加到文件的开头.我不知道它来自哪里或如何摆脱它.由于此片段是较大程序的一部分,该程序旨在自动读取文件并将其传递给另一个应用程序,因此不能手动删除.我目前的代码如下.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

void main()
{
    FILE * fout;
    int n;
    float f1;
    float f2;
    float f3;
    float npx[5208];
    float npy[5208];
    float npz[5208];
    int v1;
    int i;
/*read node cordinates fron the file */
    /*char buffer[100];*/
    fout = fopen("z1_115k.out", "r");
    /*fgets(buffer,100,fout);*/ /* skip the first line*/
    while(feof(fout)==0)
    {
        fscanf(fout, "%d %e %e %e\n", &v1, &f1, &f2, &f3);
        npx[v1]=f1;
        npy[v1]=f2;
        npz[v1]=f3;
    }
    fclose(fout); 
    fout = fopen("writeout9.txt" , "w");
    for(i=0;i<5208;i++)
    {
        fprintf(fout, …
Run Code Online (Sandbox Code Playgroud)

c file scanf

2
推荐指数
1
解决办法
65
查看次数

读取.txt文件并将数据保存为C中的矩阵

我有兴趣阅读.txt文件,并将数据保存在C中的矩阵中.

dist.txt is the following:
Distance    Amsterdam   Antwerp Athens  Barcelona   Berlin
Amsterdam   -   160 3082    1639    649
Antwerp 160 -   2766    1465    723
Athens  3082    2766    -   3312    2552
Barcelona   1639    1465    3312    -   1899
Berlin  649 723 2552    1899    -
Run Code Online (Sandbox Code Playgroud)

事实上它有更多的城市,但没关系.

我想阅读这份文件并记录距离.我试过以下代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

#define rows 6
#define cols 6

int main()
{
    FILE *nansa;
    char *buffer;
    int ret,row=0,i,j;

    char delims[]=" \t";
    char *result=NULL;

    double **mat=malloc( rows*sizeof(double*) );
    for(i=0; i<rows; i++)
    {
        mat[i]=malloc( …
Run Code Online (Sandbox Code Playgroud)

c matrix

2
推荐指数
1
解决办法
254
查看次数

读取C ++中的大型CSV文件(〜4GB)

我想读取一个大型CSV文件并将其存储到地图中。我首先读取文件,然后查看处理需要多长时间。这是我的循环:

while(!gFile.eof()){
   gFile >> data;
}
Run Code Online (Sandbox Code Playgroud)

我需要35分钟才能处理包含3500万行和6列的csv文件。有什么办法可以加快速度吗?对SO来说还很陌生,如果不能正确询问,请您道歉。

c++ csv parsing file large-files

2
推荐指数
1
解决办法
1551
查看次数

使用qsort时出现分段错误

我正在研究一个程序来读取txt文件并对字符串进行排序.

data.txt中:

jk ef ab cd bc gh fg ij hi de 
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>

int cmp(const void *p1, const void *p2) {
    return strcmp(*(const char **)p1,  *(const char **)p2);
}

int main() {
    FILE *f = fopen("data.txt", "r");
    char s[255][255];
    char tmp[255];
    int n = 0;

    while (!feof(f)) {
        fscanf(f, "%s", tmp);
        strcpy(s[n], tmp);
        n++;
    }

    fclose(f);

    qsort(s, n, sizeof(char *), cmp);

    int i = 0;
    for …
Run Code Online (Sandbox Code Playgroud)

c qsort segmentation-fault

2
推荐指数
2
解决办法
986
查看次数

在 C 中创建一个函数来读取文件但它无限循环

我基本上想打印出我制作的文件中的所有行,但它只是一遍又一遍地循环回到开始,基本上是因为我在函数中设置了fseek(fp, 0, SEEK_SET);这部分,但我知道否则我将如何放置它以完成所有其他操作行我基本上每次都回到开始。

#include<stdio.h>
#include <stdlib.h>

char *freadline(FILE *fp);

int main(){

    FILE *fp = fopen("story.txt", "r");

    if(fp == NULL){
        printf("Error!");
    }else{
        char *pInput;
        while(!feof(fp)){
            pInput = freadline(fp);
            printf("%s\n", pInput); // outpu 
        }   
    }
    
    return 0;
}

char *freadline(FILE *fp){
    int i;
    for(i = 0; !feof(fp); i++){
        getc(fp);
    }
    fseek(fp, 0, SEEK_SET); // resets to origin (?)
    char *pBuffer = (char*)malloc(sizeof(char)*i);
    pBuffer = fgets(pBuffer, i, fp);

    return pBuffer;
}

Run Code Online (Sandbox Code Playgroud)

这是我目前的工作

c

2
推荐指数
1
解决办法
103
查看次数

有没有办法在少于 3 个系统调用中测试文件结尾?

我想测试 引用的给定文件的位置是否fd位于文件末尾。例如 当前位置==文件大小。有没有办法在少于 3 个系统调用的情况下完成此操作?这 3 个调用是:

  1. 获取当前位置lseek
  2. lseek到文件末尾并存储该位置(即文件大小)
  3. 比较两者,如果不同,lseek则回到原来的位置。

c linux file-descriptor

2
推荐指数
1
解决办法
131
查看次数

fseek()之后的奇怪行为已经奏效

我遇到了一种奇怪的行为.虽然调试,当while-loop循环的第一次:去后虽然/* "data-url" *//* "data-author" */部分代码我的下一个结果Debugging windows -> Watches:

(我正在使用Code :: Blocks IDE,Ubuntu 13.04)

长度dataUrl_tempString8个字节,长度dataAuthor_tempString11个字节,长度dataName_tempString9个字节......

第1张照片

但经过/* data-name */部分代码后,我得到的结果让我困惑:

第2张照片

现在它们的大小不是8,11和9字节!

有什么事?

你能帮我找到这种行为的原因吗?


这是该函数的代码:

int SubString_Search(char *fnameNew, char *strUrl, char *strAuthor, char *strName) {

    FILE *fp;
    FILE *ofp_erase;
    FILE *ofp;
    char ch_buf;
    int count = 0;

    char dataUrl[8] = "";
    char dataAuthor[11] = "";
    char dataName[9] = "";
    char *dataUrl_tempString = …
Run Code Online (Sandbox Code Playgroud)

c debugging fseek

1
推荐指数
1
解决办法
294
查看次数