Hello.
I have a file merg.txt:
1 3 11 14
1 5 8 13
1 1 9 10
1
1
1
1
1
1
1
2
2
2
2
2
2
2
2
2
2 1 12 13
3
3
3
3
3
3
3
3
3
3

sorry..wrong button...:
anyway and a code:

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

struct tip_garaj{
	int etaj;
	int timp;
	int orastart;
	int orastop;
};

 struct tip_garaj *masini;

int main ( void )
{
 
static const char filename[] = "C:\merg.txt";
FILE *file = fopen ( filename, "r" );
 
if ( file != NULL )
{
char line [ 200 ]; 
int num[200];
char* parse;
signed int x = -1;

 
char* start;
char* end;
 
while ( fgets ( line, sizeof line, file ) != NULL ) /* citeste o linie */
{
if( (start = strchr(line,' \n')) != NULL)
{
end = strchr(start + 1 , '\n ');
}
if (start != NULL && end != NULL){
printf ("Am citit o data\n");
}
 
parse = strtok (line , " ");
while (parse != NULL){
x = x+1;
num[x] = atoi (parse);
printf (" num[%d] este %d\n" , x , num[x]);
parse = strtok (NULL , " ");

}
}
fclose ( file );
 
}
else
{
perror ( filename ); 
}
getch();
return 0;
}

my task is to take the data from the first column and store it in "etaj",the data from the second column in "timp" and so on.here is where i'm stuck. i am able only to read the file and print it...after this ,no ideea...so help please!

Edited 5 Years Ago by Narue: added code tags

struct tip_garaj{
int etaj;
int timp;
int orastart;
int orastop;
};
 
struct tip_garaj masini;

you can refer to each element of the struct tip_garaj:
masini.etaj
masini.timp
masini.orastart
masini.orastop

for an example, you can assign values to the above elements treating them as normal variables as follows:

masini.etaj = 5;
masini.timp = 7;
masini.orastart = 32;
masini.orastop = 89;

same thing applies to reading also,

int p = masini.etaj;
int q = masini.timp;
int r = masini.orastart;
int s = masini.orastop;

i hope this was what you were looking for.

yes i know that..but i don't want to manual asign,but after a data is read,then asign it automaticly to it's substruct.this i don't know how.

This article has been dead for over six months. Start a new discussion instead.