[討論] C 從txt檔讀取未知大小的矩陣
請問 如果要從txt檔讀取一未知大小的矩陣
要如何判定他有幾行幾列?
因為原本自己寫的程式碼 是要轉換一個txt檔的矩陣→稀疏矩陣
現在寫的是 要使用者輸入行列數
所以這程式使用前提就必須知道txt檔內矩陣的大小
顯得有點矛盾...
現在程式碼是這樣(要自己輸入行列值)
int main( )
{
char name[20];
printf("Please enter file name: ");
scanf("%s",name);
FILE *File_Inp = fopen(name,"r");
FILE *File_Outp = fopen("write.txt", "w");
int matrix[100][100];
int X_NumRow=0, Y_NumColumn=0, counter=0;
if(File_Inp==NULL)
{
printf("File is not exist! Please checking your file name.\n");
return(0);
}
printf("Enter the number of ""Row"" (列) : ");
scanf("%d", &X_NumRow);
printf("Enter the number of ""Column"" (行) : ");
scanf("%d", &Y_NumColumn);
scan_matrix(matrix, X_NumRow, Y_NumColumn);
write_sparse(matrix, X_NumRow, Y_NumColumn);
fclose(File_Inp); // close input file
fclose(File_Outp); // close output file
printf("Result is output to a file called ""write.txt""!!");
return(0);
}
function部份我就沒弄上來了 想問各位有沒有更好的方式 可以讀txt行列值做計算?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.118.232.71
→
05/31 22:12, , 1F
05/31 22:12, 1F
→
05/31 22:14, , 2F
05/31 22:14, 2F
→
05/31 22:53, , 3F
05/31 22:53, 3F
現在已經解決有幾列了 用
char R[50];
while( fgets(R, 50, File_Inp)!=NULL )
NumRow++;
這樣就把整個TXT內有幾列數值算出來了
行 的話 是不是可以用 strtok把空格跟負號都去掉
然後用strlen算R有幾個字元? 有其他算行的方式嗎?
※ 編輯: e158420502 來自: 140.118.232.71 (05/31 23:00)
→
05/31 23:22, , 4F
05/31 23:22, 4F