c - 从文件中获取数据并在每次新行开始时将其打印出来
问题描述
我对编程完全陌生,但需要在我的培训中运行一个程序。该程序的最终目标是从数据库中读取文件,然后将它们发送给请求它的客户。
目前我只是学习如何从文件中读取字符串并将其写入不同的文件。但我的问题是我想在每次换行时打印数据。
我使用的文件中的数据格式如下:
<DESCRIPTION>data,<DESCRIPTION>data,<DESCRIPTION>data
等
。数据是int和chars。
由于数据用“,”分隔,我想首先"<DESCRIPTION>data"
使用我在谷歌搜索时设法找到的 strtok 函数将所有内容放入子字符串中,之后我将只扫描“DESCRIPTION”部分,然后将所需的数据放入然后我会在到达数组末尾(行尾)时打印出该数组,然后移动到下一行直到文件结束。
我可以使用哪些功能来解决此问题?或者我如何通过每次需要数据时扫描行中的所有字符来设置一个不会永远花费的循环?如果我在说什么和我在做什么是两件不同的事情,我再次为自己是一个编程初学者而道歉。我已经编程了一个星期了,这就是我能做的全部了
#include <stdio.h>
#include <ctype.h>
void get9202() {
char
const * str;
const char s = ",";
char * token;
/*open database file*/
FILE * fp = fopen("datafile.dat", "r");
/*create array with all lines of data
I would like it to be able to handle unknown amounts of data.
current file is ~177000 lines of data.*/
int i = 0;
char line[i];
/*checking until end of file*/
while (fgets(line, sizeof(line), fp)) {
/*This part has to be included in the loop somehow but put in here
so that you might get a picture of what im trying to do.*/
while ( * str) {
if (!isspace( * str++))
i++;
else break;
/*not entirely sure how to exit this subloop
to print out the data and go to new line*/
}
/*trying to segment the string into an array of substrings
but dont know when to introduce x*/
token[x] = strtok(str, s);
while (token[x] != NULL) {
printf("%s\n,", token);
}
}
return result;
/* dont know how to return the file to main*/
flclose("datafile.dat");
}
如果数据如下所示:
<SYMBOL>9202.T,<SYMSTAT>2,<MSGSTAT>0,<TIME>20:50:40.905246,<SYS_DT>2018/07/19,<SYS_TIM>20:50:40.503,<SYS_TIMU>20:50:40.503236
<SYMBOL>9202.T,<SYMSTAT>2,<MSGSTAT>0,<TIME>20:51:40.000235,<SYS_DT>2018/07/19,<SYS_TIM>20:51:39.598,<SYS_TIMU>20:51:39.598597
预期的文件可能看起来像
9202.T,2,0,20:50:40.905246
9202.T,2,0,20:51:40.000235
随着想要的作品被选中,一些作品会掉下来。
解决方案
几个问题:
将声明零长度数组。
int i=0; char line[i];
fclose
永远不会执行,因为return
还fclose
需要FILE *
作为参数。return result; /* dont know how to return the file to main*/ flclose("datafile.dat");
建议:
试图将字符串分割成子字符串数组,但不
知道何时引入 x
使用fgets
withfscanf
来解析您的行,因为所有行都是相同的。
不知道如何将文件返回到 main
使用所需字段定义 astructure
并将其返回给main
。
例子:
typedef struct {
char symbol[50];
char symstat;
char msgstat;
char time[50];
}data;
data *get9202(int *numData) {
int memAllocated = 10;
data *mData = malloc(sizeof(*mData) * memAllocated);
FILE *fp = fopen("datafile.dat", "r");
char buf[3000];
int i = 0;
while (fgets(buf, sizeof buf, fp) != NULL) {
if (i == memAllocated) {
memAllocated *= 2;
void *temp = realloc(mData, sizeof( *mData) * memAllocated);
if (temp != NULL) mData = temp;
else break; //error
}
if (sscanf(buf, "<SYMBOL>%[^,],<SYMSTAT>%c,<MSGSTAT>%c,<TIME>%[^,]",
mData[i].symbol, &mData[i].symstat, &mData[i].msgstat, mData[i].time) == 4) {
i++;
} else {
printf("error\n"); //error
}
}
fclose(fp);
*numData = i;
return mData;
}
int main() {
int len = 0;
data *mData = get9202( &len);
int i = 0;
for (i = 0; i < len; i++)
printf("%s,%c,%c,%s\n", mData[i].symbol, mData[i].symstat, mData[i].msgstat,
mData[i].time);
if (mData) free(mData);
}
推荐阅读
- swift - TableViewCell 元素中的 UIMenuController
- android - 在 android studio 中编译失败
- linux - Unix 用户级别如何影响我对 docker 映像的可见性?
- android - 计算到特定时区午夜的秒数
- javascript - 在 try/catch 块中成功异步请求后执行语句
- c++ - 交错插入排序功能无法正确排序
- android - Expo with Android Studio Emulator 错误:“Error running adb: This computer is not authorized to debug the device”
- php - 文件夹中的 HTML 图像不再出现 (PHP)
- python - 如果日期是周末 - 自动重新运行该功能
- c++ - 什么方法可以创建一个 16 字节的校验和