Link Exchange
- - - - - - - - - - - - - - ผู้ให้การสนับสนุน- - - - - - - - - - - - - -
โจทย์1.ให้เปิด Notepad พิมพ์ค่าต่อไปนี้แล้ว save เป็นชื่อ data.txtมีข้อมูลดังนี้ 13 19 24 3 28 27 18 8 26 15 22 29 2 17 6 12 27 23 9 44 59 60 1 10 572.เขียนโปรแกรมอ่านค่าจะ data.txt แล้วเก็บไว้ในตัวแปรอะเรย์3.แสดงข้อมูลในตัวแปรอะเรย์ ทางหน้าจอ
/* @Author: Mr.Suppakit Thongdee @Website: www.sourcecode.in.th */ #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> int main(){ clrscr(); FILE *file; const num = 24; char buf[500]; char temp[3]; int data[num]; int count; int count_num; //Open text file file = fopen("data.txt","r"); if(!file) return 1; count_num = 0; if(fgets(buf,500,file)!=NULL){ //read data in text file printf("Data from data.txt = %s\n",buf); count = 0; strcpy(temp," "); for(int i=0; i<strlen(buf); i++){ if(buf[i] != char(32)){ //check space temp[count] = buf[i]; count++; }else{ //not space //save value to array data[count_num] = atoi(temp); //clear temp value strcpy(temp," "); count = 0; count_num++; } } //save last value to array data[count_num] = atoi(temp); }else{ printf("Error: Can not open data.txt file."); return 1; } fclose(file); printf("Result in array--------------------------\n"); //Display for(int i=0; i<=num;i++){ printf("data[%d]=%d\n",i,data[i]); } getch(); return 1; }
Home - Article - Tutorial - Sourcecode - Dev Handbook - Search - WebBoard - Links - About Us