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 8831977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:25:41+00:00 2026-06-14T08:25:41+00:00

Looking forward to help…I need to read two strings from text files and store

  • 0

Looking forward to help…I need to read two strings from text files and store them into two separate arrays. I’ve searched, and got many codes, one of which worked, so I tried to modify it to read two strings. Here is my code:

int main(){
int i = 0;
int BUFSIZE = 1000;
char* words[20];
char* words2[20];
FILE *fp = fopen("input1.txt", "r");
FILE *fp2 = fopen("input2.txt", "r");
if (fp == 0){
    fprintf(stderr, "Error while opening");
    return 0;
}
words[i] = (char*)malloc(BUFSIZE);
words2[i] = (char*)malloc(BUFSIZE);
while (fgets(words[i], BUFSIZE, fp)) {
    i++;
    words[i] = (char*)malloc(BUFSIZE);
} 
while (fgets(words2[i], BUFSIZE, fp2)) {
    i++;
    words2[i] = (char*)malloc(BUFSIZE);
} 
printf("Output: \n");
srand(time(NULL));
int j = rand()%i;
int k = (j+1)%i;
fflush(stdout);
printf("%d - %s %d -%s", j, words[j], k, words[k]); 

int x;
for(x = 0; x<i; x++)
   free(words[x]);
   free(words2[x]);
scanf("%d", x);
fclose(fp);
fclose(fp2);
return 0;
}

But it won’t work.Anyone knows why? Thank you!

  • 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-14T08:25:42+00:00Added an answer on June 14, 2026 at 8:25 am

    Reset i = 0 before second loop and enbrace your free(x) code in curly braces

    int main()
    {
        int i = 0;
        int BUFSIZE = 1000;
        char* words[20];
        char* words2[20];
        FILE *fp = fopen("input1.txt", "r");
        FILE *fp2 = fopen("input2.txt", "r");
        if (fp == 0){
            fprintf(stderr, "Error while opening");
            return 0;
        }
        words[i] = (char*)malloc(BUFSIZE);
        words2[i] = (char*)malloc(BUFSIZE);
        while (fgets(words[i], BUFSIZE, fp)) {
            i++;
            words[i] = (char*)malloc(BUFSIZE);
        } 
    
        // reset i back to zero
        i = 0; 
        while (fgets(words2[i], BUFSIZE, fp2)) {
            i++;
            words2[i] = (char*)malloc(BUFSIZE);
        } 
    
        printf("Output: \n");
        srand(time(NULL));
        int j = rand()%i;
        int k = (j+1)%i;
        fflush(stdout);
        printf("%d - %s %d -%s", j, words[j], k, words[k]); 
    
        int x;
        for(x = 0; x<i; x++){
           free(words[x]);
           free(words2[x]);
        }
        scanf("%d", x);
        fclose(fp);
        fclose(fp2);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking forward to visualize a 3D matrix into matlab from dicom files.
I am looking forward to connect two different database from different servers. Also, i
Possible Duplicate: Concatenating two QStrings with an integer i'm looking forward to create a
I am looking into the possibility of using tools in order to help me
I need help from you all in Android mobile number verification process. I have
I am looking forward to learn database normalization with help of some examples. Putting
im looking forward to build RT web apps with NodeJS. Coming from Rails, I've
I need help in hovering two HTML tags at once. Please tell how it
im looking forward for a 3rd part tool/solution that can monitor my server's network
I'm looking forward to implement a LookAt( forward, up ) method where: Vector3 forward

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.