the problem is when I separated the code into the different function and this happens
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
//------Options -- change only true / false to trigger the function
bool hard_trace = false;
bool trace = true;
/*-------------------*/
//---------Defines----------
#define MAXSIZE 100 // maximum size of the characters in txt file to be buffed
/*----------------------*/
//---------Structs-----------
struct Matrix
{
char *A[10];
int depth;
};
/*--------------------------*/
//-----Variable----------
//-- global
char *B[3];
//- struct
struct Matrix matrixs ; // create new global struct
//-
//--
/*-----------------------*/
int convertCharToNumber(char target[1])
{
int numbered = target[0] - 48;
return numbered;
}
int generateDataFromFile(){
//-- temped
int currentLine = 1;
int currentRow = 0;
//-----------------
FILE *fp;
char line[MAXSIZE];
fp = fopen("test.txt", "r");
if (fp == NULL)
{
fprintf(stderr, "%s\n", "Error reading from file");
exit(EXIT_FAILURE);
}
while (fgets(line, MAXSIZE, fp) != NULL)
{
if(hard_trace){ // if it was enabled
printf("current line : %d and length : %d\n", currentLine, strlen(line));
}
if (line[strlen(line) - 1] == '\n') // cutout the \n to make the txt easy to use
{
line[strlen(line) - 1] = '\0';
}
//appileToStruct(line,currentRow);
matrixs.A[currentRow] = line;
//if(trace) printf("%s\n", line);
currentLine++; currentRow++;
}
if(trace) printf("Total line receive from txt file : %d\n" , currentLine-1); //if it was enabled
fclose(fp);
// ----------- assign the var
matrixs.depth = currentLine - 1;
//----------------------------
//return 1;
}
void main(){
generateDataFromFile();
printf("Total : %d TXT : [%s]", strlen(matrixs.A[0]), matrixs.A[0]);
}
and the output here
Total line recieve from txt file : 3
Total : 10 TXT : []
.
But it's fine when i directly put the code in the main like this
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
//------Options -- change only true / false to trigger the function
bool hard_trace = false;
bool trace = true;
/*-------------------*/
//---------Defines----------
#define MAXSIZE 100 // maximum size of the characters in txt file to be buffed
/*----------------------*/
//---------Structs-----------
struct Matrix
{
char *A[10];
int depth;
};
/*--------------------------*/
//-----Variable----------
//-- global
char *B[3];
//- struct
struct Matrix matrixs; // create new global struct
//-
//--
/*-----------------------*/
int convertCharToNumber(char target[1])
{
int numbered = target[0] - 48;
return numbered;
}
int main()
{
//-- temped
int currentLine = 1;
int currentRow = 0;
//-----------------
FILE *fp;
char line[MAXSIZE];
fp = fopen("test.txt", "r");
if (fp == NULL)
{
fprintf(stderr, "%s\n", "Error reading from file");
exit(EXIT_FAILURE);
}
while (fgets(line, MAXSIZE, fp) != NULL)
{
if (hard_trace)
{ // if it was enabled
printf("current line : %d and length : %d\n", currentLine, strlen(line));
}
if (line[strlen(line) - 1] == '\n') // cutout the \n to make the txt easy to use
{
line[strlen(line) - 1] = '\0';
}
//appileToStruct(line,currentRow);
matrixs.A[currentRow] = line;
//if(trace) printf("%s\n", line);
currentLine++;
currentRow++;
}
if (trace)
printf("Total line recieve from txt file : %d\n", currentLine - 1); //if it was enabled
fclose(fp);
// ----------- assign the var
matrixs.depth = currentLine - 1;
//----------------------------
printf("Total : %d TXT : [%s]", strlen(matrixs.A[0]), matrixs.A[0]);
}
OUTPUT
Total line receive from txt file : 3
Total : 10 TXT : [0000111100]
Can you guy explain to me why that the first code is not working i mean why the %s in printf doesn't show the output for me and how can i get the first code to be work
When you do this:
matrixs.A[currentRow] = line;
You're assigning the address of the local array line
to an element of matrixs.A
. In the case of the second program, it prints a valid value because line
in still in scope. But in the first program line
is out of scope so matrixs.A[0]
points to invalid memory. Dereferencing this pointer invokes undefined behavior.
Also, in the second program, you may notice that every element of matrixs.A
contains the last value you read. That is again because you're storing the address of line
in each one.
You should instead make a copy of the string you read and save a pointer to the new string. That way 1) you're not storing the address of a local variable, and 2) you don't store the same address in every element of matrixs.A
.
matrixs.A[currentRow] = strdup(line);