텍스트 파일에서 읽는 것입니다. 텍스트 파일의C 프로그래밍 - 텍스트 파일에서 읽기
- 이름
- 지연 시간을 읽고 싶어 얼마나 많은 라인 (들)
- : 나는 3 개 명령 줄 인수를 가지고있다.
텍스트 파일이 끝날 때까지 사용자가 지정한 줄 번호로 해당 텍스트 파일을 읽고 싶습니다.
예를 들어 처음으로 5 줄을 읽은 다음 프로그램에서 how many line(s) do you want to read?
을 묻습니다. 나는 7
이라고 입력하면 5에서 12 행까지 읽습니다.
이것은 파일의 끝까지 반복됩니다.
#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");
}
이 코드의 문제점은 무엇입니까? (나는 하나를 본다 : 당신의 지연 루프는 CPU 집약적 인 루프이며, 그리 좋지 않다!) –
그것은 약간의 추가가 필요하다. 이 코드는 텍스트가 끝날 때까지 묻지 않습니다. –