からの読み取りは、テキストファイルからの読み取りについてです。テキストファイルのCプログラミング - テキストファイル
- 名
- 遅延時間読みたいどのように多くの行(複数可)
- : 私は3つのコマンドライン引数を持っています。
テキストファイルが終了するまで、ユーザーが指定した行番号でテキストファイルを読みたいと思っています。
たとえば、最初に5行を読み取った後、プログラムはhow many line(s) do you want to read?
と尋ねます。私は7
と入力します。
これはファイルの最後まで繰り返されます。
#include <stdlib.h>
#include <stdio.h>
#include<time.h>
#include <string.h>
void delay(unsigned int mseconds)
{
clock_t goal = mseconds + clock();
while (goal > clock());
}
int countlines(const char *filename) {
FILE *fp = fopen(filename, "r");
int ch, last = '\n';
int lines = 0;
if (fp != NULL) {
while ((ch = fgetc(fp)) != EOF) {
if (ch == '\n')
lines++;
last = ch;
}
fclose(fp);
if (last != '\n')
lines++;
}
return lines;
}
int main(int argc, char *arg[])
{
FILE *ptDosya;
char ch;
ch = arg[1][0];
int s2;
int satir = 0;
int spaceCounter=0;
int lineCount, x = 0;
lineCount = atoi(arg[3]);
s2 = atoi(arg[2]);
printf("dosya %d satir icerir.\n", countlines(arg[1]));
ptDosya = fopen(arg[1], "r");
if (ptDosya != NULL)
{
while (ch != EOF&& x < lineCount)
{
ch = getc(ptDosya);
printf("%c", ch);
if (ch == '\n')
{
delay(s2);
x++;
}
}
while (x < countlines(arg[1]))
{
printf("satir sayisi giriniz:");
scanf("%d", &lineCount);
// i don't know what should i do in this loop..
x=x+lineCount;
}
}
else {
printf("dosya bulunamadi");
}
printf("\n\nend of file!\n");
fclose(ptDosya);
return 0;
system("PAUSE");
}
このコードの問題点を教えてください。 (私は1つを見ます:あなたの遅延ループはCPU集約的なループであり、それほど良いものではありません!) –
それはいくつかの追加が必要です。このコードでは、テキストが終了するまで尋ねないと行番号を取得します。 –