Jump to content

This is driving me nuts !

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts

#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 :P
ANyway 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

#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
It's showing nothing because you read to the end of file and leave the file at the end and attempt to continue reading. There is nothing more to read. Go back to the beginning before looking for your line:
   rewind(file);
When you print this line, think about what you are doing in the nested loop: only on a newline will you forever print the newline. Rethink your check, your loops, and what you are trying to print.

#3
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
I am still unable to figure it out ! :(

#4
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
cmon cmon, anyone ?!

#5
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Post your latest attempt so it doesn't look like you're asking to be spoonfed.

#6
kmhosny

kmhosny

    Programmer

  • Members
  • PipPipPipPip
  • 133 posts
i dont think that your code ever stops since the last condition
while(n==a){
   putchar(f);
}
never ends.

or if your code does end then the condition
if(n==a){
...
}
is never true so the while is never entered
"Recursion is just a line of code"
-Karim Hosny-
My flickr

#7
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
last attempt
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
    FILE *file;
    file=fopen("test.txt", "rb");
    int newlines=0;
    int c;
    int a;
    char f;
	int n=0;
	int s=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;
	rewind(file);
    while ((f=fgetc(file))!=EOF) {
		while((s=fgetc(file))!=EOF){

		if(s=='\n'){
			n=n+1;
				while(n==a){
						putchar(f);
                                                if(s=='\n')
                                                  n=n+1;
					}
		}
		}
		
	}
	fclose(file);
    getchar();
	return 0;
}


#8
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
You haven't tried to change that nested while loop.

dcs said:

When you print this line, think about what you are doing in the nested loop: only on a newline will you forever print the newline. Rethink your check, your loops, and what you are trying to print.

kmhosny said:

i dont think that your code ever stops since the last condition
while(n==a){
   putchar(f);
}
never ends.

Think about it. You only want to print characters when you are on the particular line. In code that is relatively trivial.
      srand(time(NULL));
      [COLOR="red"]line[/COLOR] = rand() % newlines; // the line you want
      [COLOR="red"]n = 0;[/COLOR]
      while ( (ch = fgetc(file)) != EOF )
      {
         if ( [COLOR="red"]n == line[/COLOR] )
         {
            putchar(ch);
         }
         if ( ch == '\n' )
         {
            ++n;
         }
      }


#9
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
Thank you DCS ! It worked !
I'm learning some stuff here :D
+rep.
Edit : can't rep you, already did yesterday D:

#10
kmhosny

kmhosny

    Programmer

  • Members
  • PipPipPipPip
  • 133 posts
also you might think of changing the mode from rb to a+b to be able to read and write binary since the open mode in you code above is read only
"Recursion is just a line of code"
-Karim Hosny-
My flickr

#11
kmhosny

kmhosny

    Programmer

  • Members
  • PipPipPipPip
  • 133 posts
since it worked then ignore what is said :)
"Recursion is just a line of code"
-Karim Hosny-
My flickr

#12
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
Thanks for your help kmhosny, noticing that it gets stuck in while helped me to get my logic right but couldn't write it in code.

And i'm using vista so it won't let a program write to a file unless i give it admin permission.