#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
FILE *file;
file=fopen("C:/test.txt", "rb");
int newlines=0;
int c;
int a;
int f,n=0;
if(file==NULL){
printf("Error opening file");
getchar();
return 1;
}
while ((c=fgetc(file))!=EOF) {
if (c=='\n') {
newlines++;
}
}
if(newlines!=0){
newlines=newlines+1;
}
srand(time(NULL));
a=rand()%newlines;
while ((f=fgetc(file))!=EOF) {
if(f=='\n'){
n=n+1;
if(n==a){
while(n==a){
putchar(f);
}}
}
}
fclose(file);
getchar();
return 0;
}
Yea i know i'm learning a little :PANyway create a file called test.txt in C drive ( or just modify the code )
And put some lines into it with random text.
I want it to show a random line, ONLY ONE LINE. not the other, i see this code correct but its showing nothing :/
THanks in advance codecall members


Sign In
Create Account


Back to top









