Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8572053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:55:10+00:00 2026-06-11T18:55:10+00:00

void getS(char *fileName){ FILE *src; if((src = fopen(fileName, r)) == NULL){ printf(%s %s %s,

  • 0
void getS(char *fileName){
    FILE *src;
    if((src = fopen(fileName, "r")) == NULL){
        printf("%s %s %s", "Cannot open file ", fileName, ". The program is now ending.");
        exit(-1);
    }
    //char *get = " ";      

    int c = 1;
    char ch = 'x';
    while(ch!=EOF) {
        ch = fgetc(src);
        if(ch == '\n')  c++;
    }
    fseek(src, 0, SEEK_SET);
    int random = rand() % c;
    int i = 0;
    for(i = 0; i < random; i++){
        while(ch != '\n'){
            ch = fgetc(src);
        }
    }
    do{
        ch = fgetc(src);
        if(ch != '\n' && ch != EOF){
            printf("%c", ch);
        }
    }while(ch != '\n' && ch != EOF);
    printf("%c", '\n');
    fclose(src);
}

So this is my function that grabs a file and prints out a random word in the file if each word is separated by a new line.

Question 1:
Why is the random having preference to the first 2 words?

Question 2: How would I make it so I can use this function multiple times without doing the printf(“%c”, ‘\n’); because if I don’t have that in the end the previous function call just overwrites the old one.

Thanks in advance, I’ve been asking a bit today thanks for all the help stackoverflow! 🙂

P.S. using srand(time(NULL));

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:55:12+00:00Added an answer on June 11, 2026 at 6:55 pm

    Look at the logic here:

        for(i = 0; i < random; i++){
            while(ch != '\n'){
                ch = fgetc(src);
            }
        }
    

    Once you hit a newline, you won’t read any more characters, so you’re always going to print either the first or second line.

    You can fix it like this:

        for(i = 0; i < random; i++){
            ch = fgetc(src); // start by reading the first character on the line
            while(ch != '\n'){
                ch = fgetc(src);
            }
        }
    

    Jim Balter also notes that ch would best be declared as an int. This is because EOF is not considered to be a regular character.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

void return_input (void) { char array[30]; gets (array); printf(%s\n, array); } After compiling it
Consider the code: void foo(char a[]){ a++; // works fine, gets compiled //... }
void test() { unsigned char c; c = (~0)>>1 ; printf(c is %u\n,c); }
void graph::fillTable() { ifstream fin; char X; int slot=0; fin.open(data.txt); while(fin.good()){ fin>>Gtable[slot].Name; fin>>Gtable[slot].Out; cout<<Gtable[slot].Name<<endl;
in a function, that gets unsigned char && unsigned char length, void pcap_callback(u_char *args,
#include <iostream> using namespace std; void main(){ char name[20]; gets(name); cout<<name<<endl; } I can't
I have a method, -(void)validateFormInput:(UITextField *)textField that gets called when the form's submit button
void SaveFiles(out XElement Attachments) { Attachments = null; if (Uploader1.UploadedFiles.Count() > 0) { Attachments
public void button2_Click(object sender, EventArgs e) { char[] delimiters = { ',', '\r', '\n',
This an extract from a c program that should demonstrate a bufferoverflow. void foo()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.