实验平台 vs 2015,c代码,将本地txt文档读出来,放在内存,并且从最后一行倒着显示出来
#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<Windows.h>
#include<io.h>
#include<sys/types.h>
int main()
{
char buf[2048] = { 0 };
char *p = NULL;
char **tpp_1 = NULL;
int i = 0, j = 0, n = 0, k = 0, f = 0, temp_index = 0, rows = 0;
char temp[20][100] = { NULL };
char name_str[100] = { 0 };
char time_str[100] = { 0 };
char p_val_str[100] = { 0 };
char n_val_str[100] = { 0 };
int a = 1;
FILE *pfile;
//if ((pfile = fopen("D:\\wincc_par\\temp_file\\parameter_record.txt", "r")) == NULL)
//改为自己的txt文件名称可进行测试
if ((pfile = fopen("D:\\wincc_class\\p_re.txt", "r")) == NULL)
{
return 0;
}
while (!feof(pfile))
{
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), pfile);
rows++;
}
tpp_1 = (char **)malloc((rows)*(sizeof(char *)));
memset(tpp_1, 0, rows * sizeof(char *));
for (j = 0; j < rows; j++)
{
tpp_1[j] = (char *)malloc(100);
memset(tpp_1[j], 0, 100);
}
printf("rows=%d\n", rows);
fseek(pfile, 0L, SEEK_SET);
while (!feof(pfile))
{
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), pfile);
strcpy(tpp_1[i++], buf);
}
fclose(pfile);
printf("最后一行数据:%s", tpp_1[rows - 2]);
for (j = rows - 2; j >= 0; j--)
{
n = 0;
memset(temp, 0, sizeof(temp));
memset(name_str, 0, sizeof(name_str));
memset(time_str, 0, sizeof(time_str));
memset(p_val_str, 0, sizeof(p_val_str));
memset(n_val_str, 0, sizeof(n_val_str));
p = strtok(tpp_1[j], " ");
while (p)
{
strcpy(temp[n++], p);
p = strtok(NULL, " ");
}
/*sprintf(name_str, "name_%d", temp_i);
sprintf(time_str, "time_%d", temp_i);
sprintf(p_val_str, "p_val_%d", temp_i);
sprintf(n_val_str, "n_val_%d", temp_i);*/
strcpy(name_str, temp[0]);
strcpy(time_str, temp[2]);
strcat(time_str, " ");
strcat(time_str, temp[3]);
strcpy(p_val_str, temp[4]);
strcpy(n_val_str, temp[5]);
printf("name_str:%s\n", name_str);
printf("time_str:%s\n", time_str);
printf("p_val_str:%s\n", p_val_str);
printf("n_val_str:%s\n", n_val_str);
temp_index++;
if (temp_index >= 44)
{
break;
}
}
printf("temp_index:%d\n", temp_index);
for (k = 0; k < rows; k++)
{
if (tpp_1[k] != NULL)
{
free(tpp_1[k]);
tpp_1[k] = NULL;
}
}
if (tpp_1 != NULL)
{
free(tpp_1);
tpp_1 = NULL;
}
switch (a)
{
case 1:
printf("1\n");
break;
default:
break;
}
system("pause");
return 0;
}