看板 C_and_CPP 關於我們 聯絡資訊
*[1;36m開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux)*[m VS C++ *[1;36m有問題的code: (請善用置底文標色功能)*[m #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <iostream> struct FileIn { int nF; float fG; int nH; int nI; char cJ[10]; float fK; float fL; float fM; int nN; float fZ; }csvfile; int main() { FILE *fp1 = fopen( "grp_21.csv" , "r" ); if( fp1 == NULL ) { printf( "開檔錯誤" ); exit(1); } fscanf( fp1 , "%d,%f,%d,%d,%s,%f,%f,%f,%d,%f\n" , &csvfile.nF , &csvfile.fG , &csvfile.nH , &csvfile.nI , &csvfile.cJ , &csvfile.fK , &csvfile.fL , &csvfile.fM , &csvfile.nN , &csvfile.fZ , ); printf( "%s" , csvfile.cJ ); fclose(fp1); system( "pause" ); } 遇到的問題: (題意請描述清楚) 我用fscanf讀取檔案資料 一筆資料中有整數,小數點,中文 檔案中分別用逗號隔開 當我讀到中文字的時候 我設定的是%s 而跑到這項的時候 後面的資料全部都寫入cJ之中 %s之前的資料分別讀都是正確的 %s後面的資料都是空的 希望得到的正確結果: 讓%s只單獨存字元就好 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.138.178.67
phstudy:%s改成%[^,] 07/17 19:16
dann1:成功了~ 大大可以解釋一下[^,]這個的意思嗎 07/17 19:35
dann1:研究了一下好像是規定字串結尾是逗號 那^的意思是無視嗎 07/17 19:39
TsinTa:^是not的意思,[^,]代表存取逗號外的所有字元 07/17 19:44
loveme00835:regular expression 07/17 19:48
loveme00835:可以搭配fread、fwrite, 把整個結構變數都用二進位方 07/17 20:13
loveme00835:式存放在檔案中, 這樣也可以支援隨機存取 07/17 20:14
loveme00835:http://ppt.cc/@qjm 07/17 20:15
dann1:恩恩~了解 謝謝各位大大 07/17 20:29
tropical72:好強..沒看過這種用法 07/18 21:19