【发布时间】:2014-05-05 05:13:52
【问题描述】:
大家好,提前非常感谢,我想在这里完成的是正在使用 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)
还要提一下,在这里我尝试加入两个代码失败。
#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;
}
如果我不是一直在寻找更好的解释,希望我能说到点子上。 干杯。
【问题讨论】:
-
似乎是时候开始使用调试器逐行检查所有相关变量以查看真正发生了什么。