简体   繁体   English

逐行扫描每个单词或数字,并使用C打印到文件

[英]Scan each word or number in line and print to file using C

I have 2 lines (given below) in file data1.txt: 我在data1.txt文件中有2行(如下所示):

Da    KOL    -1.19503   5.27557163                      
MaB   KOL    -1.19503   5.27557163                      

I am not sure how I could extract specific words or numbers using fgets , hence I used fscanf to scan each component of the set and print them to another file. 我不确定如何使用fgets提取特定的单词或数字,因此我使用fscanf扫描集合的每个组成部分并将它们打印到另一个文件中。 The code is: 代码是:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>


main()
{
 FILE *fpt1, *fpt2;
    fpt1=fopen("data1.txt","r");
    fpt2=fopen("data2.txt","w");

    int i;
    double  ep, si;
    char *sto1, *sto2;

    for(i=0;i<2;i++)
     {  
       fscanf(fpt1,"%s\n",sto1);
       fscanf(fpt1,"%s\n",sto2);
       fscanf(fpt1,"%lf\n",&ep);
       fscanf(fpt1,"%lf\n",&si);
       fprintf(fpt2,"%s %s %2.8lf %2.8lf\n",sto1,sto2,ep,si);
     }
 fclose(fpt1);
 fclose(fpt2);  
}

But I am getting this output in file data2.txt: 但我在文件data2.txt中得到此输出:

Da (null) 0.00000000 0.00000000
KOL (null) -1.19503000 5.27557163

Although, the desired output is one with a single space in between each component of a line like: 虽然,所需的输出是一行的每个部分之间有一个空格的输出,例如:

Da KOL -1.19503 5.27557163                      
MaB KOL -1.19503 5.27557163     

Can someone please help me fix this one? 有人可以帮我解决这个问题吗?

#include <stdio.h>

//A good habit is not to use "main()"
int main(int argc, char *argv[])
{
    FILE *fpt1, *fpt2;
    fpt1=fopen("data1.txt","r");
    fpt2=fopen("data2.txt","w");

    int i;
    double ep, si;
    char sto1[100], sto2[100];  //here neeed array

    for(i=0;i<2;i++)
    {  
        fscanf(fpt1,"%s",sto1); //without '\n'
        fscanf(fpt1,"%s",sto2);
        fscanf(fpt1,"%lf",&ep);
        fscanf(fpt1,"%lf",&si);
        fprintf(fpt2,"%s %s %2.8lf %2.8lf\n",sto1,sto2,ep,si);
    }
    fclose(fpt1);
    fclose(fpt2);       

    return 0;
}

The Idea which you have is correct but I have modified/corrected your code a bit. 您的想法是正确的,但我已经对您的代码进行了一些修改/更正。 And I have used sscanf instead of fscanf . 而且我用sscanf代替了fscanf Here is the code, 这是代码,

#include<stdio.h>
#define MAX 256

int main(){

   FILE *pfile1 =NULL,*pfile2 = NULL;
   char *sto1=NULL,*sto2=NULL,line[MAX];
   double ep,si;

   pfile1 = fopen("dS.txt","r");
   pfile2 = fopen("dR.txt","w");

   if(pfile1 != NULL || pfile2 != NULL){
      while(fgets(line,255,pfile1)!=NULL){
        sscanf(line,"%s %s %Lf %Lf",&sto1,&sto2,&ep,&si);
        fprintf(pfile2,"%s %s %2.5Lf %2.8Lf\n",&sto1,&sto2,ep,si);
      }
      fclose(pfile1);
      fclose(pfile2);
  }
  return 0;
}

You used values of uninitialized variables having automatic storage duration sto1 and sto2 and invoked undefined behavior . 您使用了具有自动存储持续时间sto1sto2的未初始化变量的值,并调用了未定义的行为

Assign pointers pointing to some valid buffer to them before using them. 在使用指向有效缓冲区的指针之前,请为其分配指针。

#include<stdio.h>
#include<stdlib.h>

int main(void)
{
    FILE *fpt1, *fpt2;
    fpt1=fopen("data1.txt","r");
    fpt2=fopen("data2.txt","w");

    int i;
    double  ep, si;
    char *sto1, *sto2;

    /* allocate enough size to store data */
    sto1 = malloc(1000000);
    sto2 = malloc(1000000);

    for(i=0;i<2;i++)
    {
        fscanf(fpt1,"%s\n",sto1);
        fscanf(fpt1,"%s\n",sto2);
        fscanf(fpt1,"%lf\n",&ep);
        fscanf(fpt1,"%lf\n",&si);
        fprintf(fpt2,"%s %s %2.8lf %2.8lf\n",sto1,sto2,ep,si);
    }
    fclose(fpt1);
    fclose(fpt2);
    free(sto1);
    free(sto2);
}

Adding error checking for fopen() , malloc() and fscanf() will make this code better. fopen()malloc()fscanf()添加错误检查将使此代码更好。

Alternative way is to use statically allocated arrays instead of dynamically allocating buffer via memory management functions. 另一种方法是使用静态分配的数组,而不是通过内存管理功能动态分配缓冲区。

#include<stdio.h>

int main(void)
{
    FILE *fpt1, *fpt2;
    fpt1=fopen("data1.txt","r");
    fpt2=fopen("data2.txt","w");

    int i;
    double  ep, si;
    char sto1[1000], sto2[1000]; /* allocating too big array as automatic local variable may cause stack overflow */

    for(i=0;i<2;i++)
    {
        fscanf(fpt1,"%s\n",sto1);
        fscanf(fpt1,"%s\n",sto2);
        fscanf(fpt1,"%lf\n",&ep);
        fscanf(fpt1,"%lf\n",&si);
        fprintf(fpt2,"%s %s %2.8lf %2.8lf\n",sto1,sto2,ep,si);
    }
    fclose(fpt1);
    fclose(fpt2);
}

You can use static arrays 您可以使用静态数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM