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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:15:28+00:00 2026-06-14T09:15:28+00:00

I am working on a class project and I really need help. What I

  • 0

I am working on a class project and I really need help. What I need to realize is to read two string from either one text file, or two separate files, and store them in two arrays respectively. The strings can be of any length, but do not have to be very long. The size of each array can be adjusted automatically according to the length of the corresponding string.

I’ve searched on Stack Overflow and got some codes, I was trying one which uses malloc(). But I had troubles when I was trying to get the size of the array.

int main(){
int i = 0;

int BUFSIZE = 1000;
char* string[20];
FILE *fp = fopen("input.txt", "r");
if (fp == 0){
    fprintf(stderr, "Error while opening");
    return 0;
}

string[i] = (char *)malloc(BUFSIZE);
while (fgets(string[i], BUFSIZE, fp)) {
    i++;
    string[i] = (char *)malloc(BUFSIZE);
} 

float len=sizeof(string);
printf("%f", len);  

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

I tried to output len, but I got a constant value 80, no matter how long the string is. Besides, I don’t know how to read two strings, and store them in two separate arrays. I got errors when trying to add another string into the codes.

  • 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-14T09:15:29+00:00Added an answer on June 14, 2026 at 9:15 am

    This:

    float len=sizeof(string);
    

    Just gives you the static size of the array string which is an array of 20 pointers and each pointer is 4 bytes, 20 * 4 bytes = 80 that’s why it’s always 80. you already have the size of the array because you increment i for each string, that’s the size of the array just print i by size I mean the number of strings you allocated in the array, or the number of lines in the file.

    Edit:
    if you want to get the length of the string(s) in the array use strlen

    while (fgets(string[i], BUFSIZE, fp)) {
        i++;
        len+=strlen(string[i]);
        string[i] = (char *)malloc(BUFSIZE);    
    } 
    

    Note:
    it should be int not float but that’s not the point.

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

Sidebar

Related Questions

I'm really need help with parsing XML by TBXML. I created a demo project
A rather simple question really. I'm working on a project where I need to
Im stuck in my project here and i really need some help! I have
I am working in a project that has two main parts: a class library
I'm working on a class project to build a little Connect4 game in Java.
I'm working on a class project which is using a Kinect. According to the
I am working on a group project for class and we are trying out
I'm working on a project for class that involves generics. public interface Keyable <T>
I am working on a project that has a class 'A' that contains a
I am working on a project for my advanced Java class, and the assignment

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.