Home > Back-end >  Fprintf error
Fprintf error

Time:10-04

Programming requirements:
Set a for loop, output a TXT file at a time, and the output file name, in turn, is "3 d points - 1", "3 d points to 2", "3 d points - 3", "3 d points - 4", etc.,
Code:
#include
#include
#include

using namespace std;

Void int2str (int n, char * STR);


Int main () {
cout <"Hello, World!"

For (int j=0; J & lt; 2; J++) {
Vector Num (5);
for(int i=0; I & lt; Num. The size (); I++) {
Num=[I] j * 2;
}
Char str1 []="../output/", str2 []="3 d points -", str3 [10], str4 []=". TXT ";
Char filename [25];//used to store 3 d point set before the RANSAC. TXT file, the file name as./output/3 d points - 1. TXT

Int2str (j + 1, str3);
Strcat (filename, str1);
Strcat (filename, str2);
Strcat (filename, str3);
Strcat (filename, str4);

The FILE * fp.
Fp=fopen (filename, "w");
If (fp==NULL) {
cout <"Cann 't open the file!" }
For (int I=0; I & lt; Num. The size (); I++) {

Fprintf (fp, "% d \ n", num [I]);

}
fclose(fp);

}


return 0;
}



//converting int j string] [note: is a string array
Void int2str (int n, char * STR) {
Char buf [10]="";
int i=0;
int len=0;
Int temp=n & lt; 0? - n: n.//temp for the absolute value of n

If (STR==NULL) {
return;
}
While (temp) {
Buf [i++]=(\ % 10) + '0'.//put temp every number in buf
Temp=temp/10;
}
Len=n & lt; 0? + + I: (I);//if n is negative, you will need to more than one to store the minus sign
STR [I]=0;//the end is the end 0
While (1) {
I -;
If (buf [len - I - 1)==0) break;
STR=buf [I] [len - I - 1);//kao to a string of characters in the buf array
}
If (I==0) STR [I]='-';//if it is negative, add a minus sign

}

The problem:
When the above code for (int j=0; J & lt; 2; J++) {instead for (int j=0; J & lt; 1; {j++), it can run correctly, but once judgment conditions is greater than or equal to 2, it is an error, can not output the second file "3 d points to 2. TXT", ask why!

CodePudding user response:

You also to realize int2str, your int2str is also a lot of mistakes, why not use sprintf? It is not sweet, or sprintf_s?
 char fileName [64]; 
for(int i=0; I<10; + + I {
Memset (fileName, '\ 0', sizeof (fileName));
Sprintf (fileName, "3 d points - % d", I);
The FILE * fp.
.
}
3 d points - 1 ",

CodePudding user response:

For loop what a filename to reset?

CodePudding user response:

Thank you very much!!!!!! Sprintf before don't know how this function

CodePudding user response:

refer to the second floor YMWM_ response:
for cycle what a filename to reset?

The statement is memset
  • Related