我对编程完全陌生,但需要让程序运行作为我培训的一部分。该程序的最终目标是从数据库读取文件,然后将它们发送给请求它的客户端。
目前我只是学习如何从文件中读取字符串并将其写入不同的文件。但我的问题是我想在每次换行时打印数据。
我使用的文件中的数据采用以下格式:
<DESCRIPTION>data,<DESCRIPTION>data,<DESCRIPTION>data
等等
数据既可以是int,也可以是char。
由于数据是用“,”分隔的,我想首先将所有"<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");
建议:
trying to segment the string into an array of substrings but dont
know when to introduce x
使用 fgets
和 fscanf
来解析您的行,因为所有行都是相同的。
dont know how to return the file to main
使用所需字段定义一个结构
并将其返回到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);
}
关于c - 从文件中获取数据并在每次新行开始时打印出来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56592908/