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

  • Home
  • SEARCH
  • 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 8329969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:58:13+00:00 2026-06-09T01:58:13+00:00

I need to put a string (from a file) in a matrix and print

  • 0

I need to put a string (from a file) in a matrix and print out the result. I have some issue in understanding the right way to do this so:

#include <stdio.h>
#include <string.h>

int main (int argc, char *argv[])
{
    const int MAX = 50;
    char mat[MAX][MAX];
    char str[MAX];
    char word[MAX];
    int row = 0;
    int i = 0;

    FILE * fp;

    fp = fopen ("file.txt", "r");

    if (fp == NULL)
        printf ("Error!\n");

    while (fgets(str, MAX, fp) != NULL)
    {
        sscanf (str, "%s\n", word);

        strcpy(mat[i][0], word);

        row++;
    }

    for (i = 0; i <= row; i++)
    {
            puts(mat[i][0]);
    }

    return 0;
}

I’m obliviously doing something wrong but… what?

I have a file like this:

One
Two
Three
Four
Five
Six
Hello
  • 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-09T01:58:15+00:00Added an answer on June 9, 2026 at 1:58 am

    If you compile this with gcc, it will give you two warnings: each warning points to one of the three major errors in the code:

    main.c: In function 'main':
    main.c:24: warning: passing argument 1 of 'strcpy' makes pointer from integer without a cast
    main.c:31: warning: passing argument 1 of 'puts' makes pointer from integer without a cast
    

    Each of those line numbers — 24 and 31 — is a line where you’re using mat[i][0], which is a character, when you should instead use mat[i], which is a character array. Fix those, and then there’s just one problem: you use i, which is always 0, in the while loop. Use row, which is incremented as the row progresses, and the program should work exactly as designed.

    There are a couple of other things I would change to improve the program: your while loop reads a string into one buffer, copies it into a second buffer, then copies it into the matrix; you could just scan it directly into the matrix and be done with it!

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

Sidebar

Related Questions

I have a string, for example; llama,goat,cow and I just need to put a
I need to open an html file from the program root directory and have
I have a XML String retrieved from a XML file. I can't access the
I have a ListBox which I put some files, if the file is not
I show a unicode string from an xml file in a QLabel. I need
I'm reading data from a file that I need to be put into my
I have some reports in html file. I need to place them to excel
i need to put images into a json string.. first is that possible? i
I need to put some links in one TextView . For better user experience,
I need to put a button in the bottom right of an otherwise empty

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.