解答 fprintf玄學報錯

2021-10-03 21:14:52 字數 2341 閱讀 4248

#include

#include

#include

using

namespace std;

void

int2str

(int n,

char

*str)

;int

main()

char str1=

"../output/"

, str2=

"3d points-"

, str3[10]

, str4=

".txt"

;char filename[25]

;//用來儲存ransac之前的3d點集的.txt檔案,檔名如./output/3d points-1.txt

int2str

(j+1

, str3)

;strcat

(filename, str1)

;strcat

(filename, str2)

;strcat

(filename, str3)

;strcat

(filename, str4)

; file *fp;

fp =

fopen

(filename,

"w");if

(fp ==

null

)for

(int i =

0; i < num.

size()

; i++

)fclose

(fp);}

memset

(filename,

'\0'

,sizeof

(filename));

return0;

}//將整型數j轉換成字串[注:字串就是字元陣列]

void

int2str

(int n,

char

*str)

while

(temp)

len = n <0?

++i : i;

//如果n是負數,則需要多一位來儲存負號

str[i]=0

;//末尾是結束符0

while(1

)if(i ==

0) str[i]

='-'

;//如果是負數,新增乙個負號

}

上述**較之前的也就是在fclose(fp);之後加入了語句memset(filename, '\0', sizeof(filename)),這條語句起的作用是清零字元陣列filename。

#include

#include

#include

using

namespace std;

intmain()

char str1=

"../output/3d points-"

, str2[10]

, str3=

".txt"

;char filename[25]

;sprintf

(str2,

"%06d"

, j+1)

; cout << str2 << endl;

strcat

(filename, str1)

;strcat

(filename, str2)

;strcat

(filename, str3)

; cout << filename << endl;

file *fp;

fp =

fopen

(filename,

"w");if

(fp ==

null

)for

(int i =

0; i < num.

size()

; i++

)fclose

(fp)

;memset

(filename,

'\0'

,sizeof

(filename));

}return0;

}

顯然美觀多了!!!

輸出結果如下:

圖1

圖2

fprintf用法詳解

相關函式 printf,fscanf,vfprintf 標頭檔案 include 定義函式 int fprintf file stream,const char format,函式說明 fprintf 會根據引數format 字串來轉換並格式化資料,然後將結果輸出到引數stream 指定的檔案中,直...

fprintf 函式用法

fprintf 函式與fscanf 函式的作用於prinf 和scanf 的作用類似,都是格式化讀寫函式。只有一點不同 fprintf和fscanf函式的讀寫物件不是終端而是磁碟檔案。他們的一般呼叫方式為 fprintf 檔案指標,格式字串,輸出列表 fscanf 檔案指標,格式字串,輸入列表 fp...

fprintf與stderr stdout的使用

include void main 上面程式編譯成fprint檔案,執行顯示如下 soyo8888can t open it soyo15can t open it soyocan t open it 若將輸入重定向到乙個temp.txt檔案中,執行 fprint temp.txt 結果如下 soy...