作者rmp4aup6 (嘿嘿)
看板C_and_CPP
標題Re: [問題] fseek的問題
時間Wed May 22 17:25:07 2013
不好意思直接貼上我寫的code好了,
int main()
{
FILE *fptr, *fptr2;
int i = 0, start;
char string[MAX];
char ch;
fptr2 = fopen("new2.txt", "w+");
if(!fptr2)
{
printf("open failed\n");
exit(1);
}
// read user input data
while((ch=getchar())!=ENTER && i < MAX)
{
string[i++] = ch;
}
// write user data into new1.txt
fwrite(string, sizeof(char), i, fptr2);
fseek(fptr2, -1, SEEK_CUR);
printf("%d\n",(int)ftell(fptr2));
printf("i = %d\n", i);
int asd;
asd = fseek(fptr2, -1, SEEK_CUR);
printf("%d\n",(int)ftell(fptr2));
printf("asd = %d\n", asd);
int bytes;
char string1[MAX];
bytes = fread(string1, sizeof(char), MAX, fptr2);
printf("total character = %d\n", bytes);
if (bytes < MAX)
{
string[bytes] = '\0';
}
printf("string = %s\n", string);
fclose(fptr2);
return 0;
}
大致上是這樣,若input為hello,output就是為h。
但我原本以為會是o。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 60.250.129.63
→ elba:fread進string1,應該要秀string1的內容 05/22 18:04
→ rmp4aup6:謝謝樓上...對不起,是我沒注意到。.....orz 05/22 19:01