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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:33:12+00:00 2026-05-21T11:33:12+00:00

Hi there I have to read data from text file into the array in

  • 0

Hi there I have to read data from text file into the array in structure and then start multiple threads to compute it and finally show the results .now i have worked out the File read part and Threading part as well as the display part .

What remains is the mysterious BUG that When i call my ReadFile function from MyFunctions.C then it reads the file well and displays well But if i call the same function in same file but call from other .c file then it shows garbage values.I tried everything but cannot trace the BUG following is my complete program

myheader.h

struct Matrix {
    int m1[10][10];
    int row;
    int mult[10][10];
};

void *CalculateSquare(void *arguments);

Main.c

#include <stdio.h>
#include<stdlib.h>
#include <pthread.h>
#include <semaphore.h>

#include "myheader.h"

#define Row 4
#define Column 4


int main()
{
    pthread_t pth[4];

    struct Matrix args;

    int i=0,j=0,k=0;

    ReadFromFile(&args);

    printf("Matrix is :\n");
        for(i=0;i<4;i++)
    {
        for(j=0;j<4;j++)
        {
            printf("%d\t",(int)args.m1[i][j]);
        }
    printf("\n");
    }
       ///////////////////////// Create New Thread and Store its Id in an array for future Use//////////////////
    for(i=0;i<Row;i++)
    {
        args.row = i;
        pthread_create(&pth[i],NULL,CalculateSquare,(void *)&args);
    }
    ///////////////////////// Join All threads so that program waits for completion of each/////////////////    

    for(i=0;i<Row;i++)
    {
        pthread_join(pth[i],NULL);
    }
    printf("Waiting for Thread to Complete\n");

    /////////////////////// Display the Matrix ////////////       
    printf("Square of the Matrix is :\n");
        for(i=0;i<Row;i++)
        {
            for(j=0;j<Column;j++)
                printf("%d\t",args.mult[i][j]);
            printf("\n");
        }



    return 0;

}

MyFunctions.C

#include <stdio.h>
#include<stdlib.h>

#include "myheader.h"

struct Matrix ReadFromFile(struct Matrix args)
{


    int col = 4, row = 4, i, j;
                int  temp;  
    FILE *fin;
    fin=fopen("test.txt","r");
    if (!fin)
        perror("Can't open input");

    //args.array = (int **)(malloc(sizeof(int**) *row ));
    for(i=0;i<4;i++)
    {
        //args.array[i] = (int **)(malloc(sizeof(int**) * col));
        for(j=0;j<4;j++)
        {

            fscanf(fin,"%d \n",&temp);
            args.m1[i][j] = (int)temp;
        }
    }
    fclose(fin);

for(i=0;i<4;i++)
    {
        for(j=0;j<4;j++)
        {
            printf("%d\t",(int)args.m1[i][j]);
        }
    printf("\n");
    }

return args;
}
/* This is our thread function.It takes the Row to Process  */
void *CalculateSquare(void *arguments)
{
    struct Matrix args =*((struct Matrix *) arguments);

    int rowToProcess;
    int j = 0;
    int k = 0;

    rowToProcess = (int)args.row;
    printf("Thread calculating Row Number %d\n",rowToProcess);

    for(j=0;j<4;j++)
        {
        args.mult[rowToProcess][j]=0;
            for(k=0;k<4;k++)
        {
                //args.mult[rowToProcess][j] += (int)(args.m1[rowToProcess][k]*args.m1[k][j]);      
        }
        }   
//  sleep(5);
    return NULL;
}

Output is

1   2   3   4   
5   6   7   8   
9   10  11  12  
13  14  15  16  
Matrix is :
2   2   -1  0   
0   2   1   4242343 
3   -1075858606 1   0   
4193048 -1075858606 4242341 0   
Thread calculating Row Number 1
Thread calculating Row Number 2
Thread calculating Row Number 3
Thread calculating Row Number 3
Waiting for Thread to Complete
Square of the Matrix is :
0   909653609   0   0   
0   0   0   0   
0   0   0   15868726    
15892704    134513372   -1217004872 15949812    
  • 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-05-21T11:33:13+00:00Added an answer on May 21, 2026 at 11:33 am

    In your mainfile you call ReadFromFile(&args); – so the function argument is a pointer to a struct Matrix. But you function itself has this declaration:

    struct Matrix ReadFromFile(struct Matrix args)
    

    This means that it needs a struct Matrix – not a pointer to it. You you may want to change this argument into a pointer.
    This will of course also mean that you need to change the access to this struct from args.m1[i][j] = (int)temp; to args->m1[i][j] = (int)temp;

    Edit: Also, if you posted your whole header file, you missed the declaration of ReadFromFile.

    To be honest, I wonder why your code actually compiled in the first place with those errors, you might want to output all warnings while compiling (-Wall flag for gcc)

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

Sidebar

Related Questions

I am trying to read in data from a text file (the time). and
From the .net 4.0 previews I have read until now there has been lots
i have a text file with the following data: Calculated Concentrations 30.55 73.48 298.25
I'm trying to read the contents of a text file into the attributes of
I have a text file with a rather large amount of data of about
Is there a way to have Linux read ahead when cloning a disk? I
I have been running into OutOfMemory Exceptions while trying to load an 800MB text
My problem is pretty simple; I have text file filled with double precision floating
Lets say, I have the following text file: Listing 1: Endianess=little AddressModel=32 typedef struct{
So I've developed a system that saves database insert/update commands into a text file

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.