Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何读取文件并将每个读取的行拆分为变量或数组_C_String_File_Split - Fatal编程技术网

C 如何读取文件并将每个读取的行拆分为变量或数组

C 如何读取文件并将每个读取的行拆分为变量或数组,c,string,file,split,C,String,File,Split,大家好,提前感谢,我在这里要完成的是,我正在阅读一个带有ansi C的文件,该文件包含文本,每行包含一个字符串,如下所示: andreuga|460325945878913024|Y sorry por los que no querían pero ITESO ahí te voy|1398585232|0|0 我已经在做的是读取file.txt并将该字符串拆分为以下输出: res[0] = andreuga res[1] = 460325945878913024 res[2] = Y s

大家好,提前感谢,我在这里要完成的是,我正在阅读一个带有ansi C的文件,该文件包含文本,每行包含一个字符串,如下所示:

andreuga|460325945878913024|Y sorry por los que no querían pero ITESO ahí te voy|1398585232|0|0 
我已经在做的是读取file.txt并将该字符串拆分为以下输出:

res[0] = andreuga
res[1] = 460325945878913024
res[2] = Y sorry por los que no querÝan pero ITESO ahÝ te voy
res[3] = 1398585232
res[4] = 0
res[5] = 0
res[6] = (null)
所以我要做的是在读取每一行的同时读取文件并拆分字符串,然后将该值保存到一个结构中,这样以后我就可以使用该结构并使用我拥有的另一个函数插入到数据库中。但我的主要问题是在读取文件中的每一行时拆分字符串。代码如下:

#include <string.h>

int main(){
    char    str[]= "andreuga|460325945878913024|Y sorry por los que no querían pero ITESO ahí te voy|1398585232|0|0";
    char ** res  = NULL;
    char *  p    = strtok (str, "|");
    int n_spaces = 0, i;


    /* split string and append tokens to 'res' */

    while (p) {
      res = realloc (res, sizeof (char*) * ++n_spaces);

      if (res == NULL)
        exit (-1); /* memory allocation failed */

      res[n_spaces-1] = p;

      p = strtok (NULL, "|");
    }

    /* realloc one extra element for the last NULL */

    res = realloc (res, sizeof (char*) * (n_spaces+1));
    res[n_spaces] = 0;

    /* print the result */

    for (i = 0; i < (n_spaces+1); ++i)
      printf ("res[%d] = %s\n", i, res[i]);

    /* free the memory allocated */

    free (res);

    return 0;
}
///////////////////////OUTPUT:
res[0] = andreuga
res[1] = 460325945878913024
res[2] = Y sorry por los que no querÝan pero ITESO ahÝ te voy
res[3] = 1398585232
res[4] = 0
res[5] = 0
res[6] = (null)
#包括
int main(){
char str[]=“andreuga | 460325945878913024 | Y sorry por los que no querían pero ITESO ahíte voy | 13985232 | 0 | 0”;
字符**res=NULL;
char*p=strtok(str,“|”);
int n_空间=0,i;
/*拆分字符串并将标记附加到“res”*/
while(p){
res=realloc(res,sizeof(char*)*++n_空格);
如果(res==NULL)
退出(-1);/*内存分配失败*/
res[n_空间-1]=p;
p=strtok(空,“|”);
}
/*realloc为最后一个NULL添加一个额外元素*/
res=realloc(res,sizeof(char*)*(n_空格+1));
res[n_空间]=0;
/*打印结果*/
对于(i=0;i<(n_空格+1);+i)
printf(“res[%d]=%s\n”,i,res[i]);
/*释放分配的内存*/
免费(res);
返回0;
}
///////////////////////输出:
res[0]=andreuga
res[1]=460325945878913024
很抱歉,我不知道该怎么办
res[3]=13985232
res[4]=0
res[5]=0
res[6]=(空)
还要提到的是,在这里,我试图加入这两个代码的尝试失败了

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include "libpq-fe.h"

#define LONG_MAX_LINEA  1024
#define NOM_ARCHIVO  "twitsDB.txt"


typedef struct tweet
{
 int  IDTweet;
 char IDCreator[20];
 char IDSentimentAnalysis[20];
 char HashTag[141];
 char Content[141];
 char CreationDate[30];    // time.h!!!
 char Retweet[20];
 char Favorites[20];
}Tweet;

int main(void)
{
   FILE *entrada;
   char linea[LONG_MAX_LINEA];

   char str[200];
   char ** res  = NULL;
   char *  p    = strtok (str, "|");
   int n_spaces = 0, i;
   int j = 0;

   printf("\n programa para leer una archivo");
   printf("\n-------------------------------");

   if ((entrada = fopen(NOM_ARCHIVO, "r")) == NULL){
      perror(NOM_ARCHIVO);
      return EXIT_FAILURE;
   }

   //Tweet tweet1;
   while (fgets(linea, LONG_MAX_LINEA, entrada) != NULL)
   {
      printf("%d %s", j,linea);


      //
        str[j] = linea;
        /* split string and append tokens to 'res' */

        while (p) {
          res = realloc (res, sizeof (char*) * ++n_spaces);

          if (res == NULL)
            exit (-1); /* memory allocation failed */

          res[n_spaces-1] = p;

          p = strtok (NULL, "|");
        }

        /* realloc one extra element for the last NULL */

        res = realloc (res, sizeof (char*) * (n_spaces+1));
        res[n_spaces] = 0;

        /* print the result */

        for (i = 0; i < (n_spaces+1); ++i)
          printf ("res[%d] = %s\n", i, res[i]);

        /* free the memory allocated */

        free (res);
      //

//      strcpy(tweet1.Content, "prueba tweet");
//      strcpy(tweet1.IDCreator, "1");
//      strcpy(tweet1.Favorites, "1");
//      strcpy(tweet1.Retweet, "1");
//      strcpy(tweet1.CreationDate, "2014-01-01");
//      strcpy(tweet1.HashTag, "1");
//      strcpy(tweet1.IDSentimentAnalysis, "1");



      //insertTweet(tweet1);
      // llenar la struct tweet

        j++;
        break;
   }


   fclose(entrada);
   puts("eso es todo el archivo");

   return EXIT_SUCCESS;
}
#包括
#包括
#包括
//#包括“libpq fe.h”
#定义长\u最大\u直线1024
#定义NOM_ARCHIVO“twitsDB.txt”
typedef结构tweet
{
int-IDTweet;
char-IDCreator[20];
字符ID实体分析[20];
字符标签[141];
煤焦含量[141];
char CreationDate[30];//time.h!!!
字符转发[20];
char收藏夹[20];
}推特;
内部主(空)
{
文件*entrada;
字符线[LONG_MAX_linea];
char-str[200];
字符**res=NULL;
char*p=strtok(str,“|”);
int n_空间=0,i;
int j=0;
printf(“\n程序段或文件”);
printf(“\n----------------------”;
如果((entrada=fopen(NOM_ARCHIVO,“r”))==NULL){
佩罗尔(姓名);
返回退出失败;
}
//推特推特1;
while(fgets(linea、LONG\u MAX\u linea、entrada)!=NULL)
{
printf(“%d%s”,j,linea);
//
str[j]=linea;
/*拆分字符串并将标记附加到“res”*/
while(p){
res=realloc(res,sizeof(char*)*++n_空格);
如果(res==NULL)
退出(-1);/*内存分配失败*/
res[n_空间-1]=p;
p=strtok(空,“|”);
}
/*realloc为最后一个NULL添加一个额外元素*/
res=realloc(res,sizeof(char*)*(n_空格+1));
res[n_空间]=0;
/*打印结果*/
对于(i=0;i<(n_空格+1);+i)
printf(“res[%d]=%s\n”,i,res[i]);
/*释放分配的内存*/
免费(res);
//
//strcpy(tweet1.Content,“prueba tweet”);
//strcpy(tweet1.IDCreator,“1”);
//strcpy(tweet1.1);
//strcpy(tweet1.Retweet,“1”);
//strcpy(tweet1.CreationDate,“2014-01-01”);
//strcpy(tweet1.HashTag,“1”);
//strcpy(tweet1.idEntityAnalysis,“1”);
//插入tweet(tweet1);
//llenar la struct推特
j++;
打破
}
fclose(entrada);
看跌期权(“eso es todo el ARCHVO”);
返回退出成功;
}
希望我能开门见山,如果不是的话,我会不断地寻找更好的解释。 干杯

真奇怪。。。 您声明str(第77行):

然后将strtok应用于str上的垃圾值(因为您没有初始化它)。。。(第79行)

然后在没有正确启动变量的情况下使用p(第101行):

也许问题就在那里的某个地方

================================================================

编辑:

首先,您似乎试图在str中保存linea,因此,您需要将str的声明更改为:

char str[LONG_MAX_LINEA];
因为您需要确保目标字符串将有足够的空间

那么您就不能尝试分配doingstr[j]=linea这是错误的 因此,该行应更改为:

strcpy(str,linea);
其次,执行strtotrash值没有意义,因此将p声明更改为:

char *  p    = NULL;
然后,在这段时间之前,你写下:

p = strtok (str, "|");

它现在能用了吗?

我不知道我是否真的有点卡住了,有些沮丧,在与源代码的链接中,只有分离的部分能用,但当我尝试将源代码与分离的部分和读取文件的部分结合起来时,我就是做不到,也许从一开始这是一个错误的方法,但主要是我尝试在拆分每一行中读取一个文件,在读取后,拆分每一行,将其保存到struct并调用一个函数插入bd,如果有更好的解决方案我想我没有提到,但这两个代码都独立工作,一个拆分给定的字符串,另一个插入数据库,就在我尝试连接这两个代码时,我不明白我做错了什么,而且我在C方面没有那么丰富的经验,这就是我的答案!声明“char str[200];”,因为没有初始化它,所以它包含垃圾值。然后将strtok应用于垃圾桶并将其分配给p(显然是错误的)。然后在你的while中使用这个值。你应该把strtok应用到你从文件中读到的东西上……是的,你完全正确,我在检查代码,结果就是,我没有正确地拆分。我真的很困惑,但现在似乎是时候开始使用调试器逐行检查代码了
strcpy(str,linea);
char *  p    = NULL;
p = strtok (str, "|");