如何将一个文本文件行分割成不同的行输出c程序



我有一个文本文件,其中包含购买很少商品的人的订单数据,它们存储在文本文件中的一行中。如何打印出单行文本文件内容,但我希望输出在不同的行中打印?

的例子:文本文件内容:

wana DR02冰美式8.90 SW03牛肉培根6.50 SK01辣味薯条5.00

输出:

  • 歌曲名
  • DR02 Iced Americano 8.90
  • SW03牛肉培根6.50
  • SK01 hot spicy Chips 5.00
void AddOdr()
{
FILE *drk;
FILE *sdwh;
FILE *cke;
FILE *snck;
FILE *odr;
FILE *tmp;
int p;
char username[50];
char code[50];
char fname[50];
char lname [50];
float price;
float total = 0;
float amt;
float bal;
int quantity;
int i = 1;
int j;
int y;
int menu;
char t;
tmp = fopen ("temp.txt","w");
printf("nnEnter your username for account verification: ");
scanf("%s", &username);
fprintf(tmp,"%st",username);
drk = fopen("drink.txt","r");
sdwh = fopen("sandwich.txt","r");
cke = fopen("cake.txt","r");
snck = fopen("snack.txt","r");
fflush(stdin);
printf("ntDISCLAIMER TO ALL CUSTOMERS!nYOU CAN PURCHASE ONLY ONE ITEM FROM EACH CATEGORY PER ORDERn(MAX 4 ITEMS PER ORDER)n");
printf("nHow many items would you like to buy?nn:");
scanf("%d",&j);
while (i<=j)
{
printf("nnWhich category would you like to buy?n1.Drinksn2.Sandwichesn3.Cake and Pastriesn4.Snacksnn:");
scanf("%d", &p);
switch (p){
case 1: printf("nEnter the code you want to purchase: ");
scanf("%s",&code);
while (fscanf (drk, "%s %s %s %fn",&prc.code,&prc.fname,&prc.lname,&prc.price) != EOF){
if (strcmp(code,prc.code) == 0){
printf ("%s", code);
printf ("n%.2f", prc.price);
fprintf (tmp, "%st%st%st%.2ft",prc.code,prc.fname,prc.lname,prc.price);
total += prc.price;
}
}
break;
case 2: printf("nEnter the code you want to purchase: ");
scanf("%s",&code);
while (fscanf (sdwh, "%s %s %s %fn",&prc.code,&prc.fname,&prc.lname,&prc.price) != EOF){
if (strcmp(code,prc.code) == 0){
printf ("%s", code);
printf ("n%.2f", prc.price);
fprintf (tmp, "%st%st%st%.2ft",prc.code,prc.fname,prc.lname,prc.price);
total += prc.price;
}
}
break;
case 3: printf("nEnter the code you want to purchase: ");
scanf("%s",&code);
while (fscanf (cke, "%s %s %s %fn",&prc.code,&prc.fname,&prc.lname,&prc.price) != EOF){
if (strcmp(code,prc.code) == 0){
printf ("%s", code);
printf ("n%.2f", prc.price);
fprintf (tmp, "%st%st%st%.2ft",prc.code,prc.fname,prc.lname,prc.price);
total += prc.price;
}
}
break;
case 4: printf("nEnter the code you want to purchase: ");
scanf("%s",&code);
while (fscanf (snck, "%s %s %s %fn",&prc.code,&prc.fname,&prc.lname,&prc.price) != EOF){
if (strcmp(code,prc.code) == 0){
printf ("%s", code);
printf ("n%.2f", prc.price);
fprintf (tmp, "%st%st%st%.2ft",prc.code,prc.fname,prc.lname,prc.price);
total += prc.price;
}
}
break;
}
i++;
}
fprintf(tmp,"%.2fn",total);
fclose(tmp);
printf ("nnTotal price is RM%.2f", total);
printf("nProceed with payment? nn1.Yesn2.Cancel Ordernn: ");
scanf("%d",&y);
switch (y){
case 1: printf("Enter the amount you will be paying (RM): ");
scanf("%f", &amt);
bal = amt - total;
if (total > amt){
printf("nAmount entered is insufficient.nPurchase attempt unsuccessful!n");
printf ("...redirecting you back to Order Page...");
CustOdr();
}
else if (amt >= total){
printf("nYour balance is RM%.2f",bal);
printf("nThank you for purchasing from Dosta Coffee Shop!");
tmp = fopen ("temp.txt","r");
if (tmp == NULL){
printf("n Unable to open file.n");
quit();
}
odr = fopen("order.txt","a");
if (odr == NULL){
printf("n Unable to open file.n");
quit();
}
printf("nnYOUR ORDER:n");

while ((t = fgetc(tmp)) != EOF){
printf("%c",t); // need to print this in separate lines
fputc(t, odr);
}
fclose(odr);
fclose (tmp);
remove ("temp.txt");

}
else {
printf("Error occured!");
}
}
}

可以使用fgets()函数将文本文件中的每行读入字符数组(缓冲区),然后使用strtok()函数将该行拆分为单独的令牌。最后,您可以在单独的行上打印每个标记。

下面是一个简单的例子:

#include <stdio.h>
#include <string.h>
#define MAX_LEN 1024
int main()
{
FILE *fp;
char line[MAX_LEN];
char *token;
fp = fopen("file.txt", "r");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
while (fgets(line, MAX_LEN, fp) != NULL) {
// remove newline character from string
line[strcspn(line, "rn")] = 0;
// split string into tokens using space as delimiter
token = strtok(line, " ");
// print each token on a separate line
while (token != NULL) {
printf("%sn", token);
token = strtok(NULL, " ");
}
}
fclose(fp);
return 0;
}

最新更新