I have 2 lines (given below) in file data1.txt:
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. 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:
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
. 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 .
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.