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

The Archive Base Latest Questions

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

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> int main() { int a =

  • 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main()
{
int a = 0, len1 = 0, len2 = 0;
int BUFSIZE = 1000;
char *string1[1];
char *string2[1];
FILE *fp1 = fopen("input1.txt", "r");
FILE *fp2 = fopen("input2.txt", "r");
if ((fp1 == 0)||(fp2 == 0))
{
    fprintf(stderr, "Error while opening");
    return 0;
}
string1[a] = (char *)malloc(BUFSIZE);
string2[a] = (char *)malloc(BUFSIZE);
fgets(string1[0], BUFSIZE, fp1);
fgets(string2[0], BUFSIZE, fp2);
len1=strlen(string1[0]);
len2=strlen(string2[0]);

printf("%c\n", string1[0][4]);
printf("Output: \n");
srand(time(NULL));
printf("%s\n", string1[0]);
printf("%s\n", string2[0]);
printf("\n");
printf("%d %d", len1, len2); 
printf("\n");

free(string1[0]);
free(string2[0]);
int x=0;
scanf("%d", &x);
fclose(fp1);
fclose(fp2);
return 0;
}

I need to read a string from a file and store it in an array. I need to read only one line of string, and each element of the array should be a character of the string. For example, if I read ‘ABCDABC’ into an array, then array[3] should be ‘D’. But I don’t really know how to do it, I modified some other’s code, and get the above code. But I don’t want to involve pointers and address stuff in my code. So could anyone tell me how to realize it without using pointers? 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-14T13:04:16+00:00Added an answer on June 14, 2026 at 1:04 pm

    Instead of char * and malloced memory, you can also use plain char[N]. The changes are small, since a char[N] is automatically converted to a pointer to its first element when it is passed to a function (likje fgets or strlen). The code would also only minimally change if you used plain char* for string1 and string2, char *string1 = malloc(BUFSIZE);, the main difference here would be the malloc and the free at the end.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
        int a = 0, len1 = 0, len2 = 0;
        int BUFSIZE = 1000;
    
        // char *string1;
        // char *string2;
    
        // instead of one-element arrays of char*, let's just use char arrays
    
        char string1[BUFSIZE];
        char string2[BUFSIZE];
    
        // One pointer to check whether fgets succeeded
        char *suc;
    
        FILE *fp1 = fopen("input1.txt", "r");
        FILE *fp2 = fopen("input2.txt", "r");
        if ((fp1 == 0)||(fp2 == 0))
        {
            fprintf(stderr, "Error while opening");
            return 0;
        }
        // No need for malloc, we have char arrays
        // string1 = (char *)malloc(BUFSIZE);
        // string2 = (char *)malloc(BUFSIZE);
        suc = fgets(string1, BUFSIZE, fp1);
        if (!suc) {
            // fgets failed, what now? exit?
            return EXIT_FAILURE;
        }
        suc = fgets(string2, BUFSIZE, fp2);
        if (!suc) {
            // see above
            return EXIT_FAILURE;
        }
        len1=strlen(string1);
        len2=strlen(string2);
    
        // is the read string long enough?
        if (len1 > 4) {
            printf("%c\n", string1[4]);
        }
        printf("Output: \n");
        srand(time(NULL));
        printf("%s\n", string1);
        printf("%s\n", string2);
        printf("\n");
        printf("%d %d", len1, len2); 
        printf("\n");
    
        // free is only for m/c/re-alloced memory
        // free(string1);
        // free(string2);
        int x=0;
        scanf("%d", &x);
        fclose(fp1);
        fclose(fp2);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { // int char str[40],ch; FILE*fp,*fp1,*fp2; fp=fopen(ide_input,w); fp1=fopen(error_log,w); fp2=fopen(lex_output,w);
#include <string.h> #include <stdlib.h> #include <stdio.h> int main(void) { unsigned char *stole; unsigned char
#include <stdlib.h> #include <stdio.h> #include <math.h> #include <string.h> extern char **environ; int global_x =
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> int main() { int i =
#include<stdlib.h> #include<string.h> #include<stdio.h> int pstrcmp( char **p,char **q) { return strcmp(*p,*q) ; } int
#include<iostream> #include<stdlib.h> #include<string.h> #include<stdio.h> using namespace std; union type{ int a; char b; int
Please see this piece of code: #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int i
#include<stdio.h> #include<stdlib.h> char* re() { char *p = hello; return p; } int main()
#include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { if(argc != 2) return
Possible Duplicate: Getting Segmentation Fault // reverse a string #include`<stdlib.h>` #include`<stdio.h>` #include`<string.h>` #include`<math.h>` int

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.