hi all.
i need help...
1.how do i copy a line in a text file into a struct.
2. the text file i have is this:

10 SasonSasoniBenSason 100 100
5 Pinokio 70 90
20 JohnRambo 50 87
3 Elvis 3 77
50 JamesBond 17 1
40 BillGates 91 8

i need to sort it(without using a sort function like buble sort)
how do i refer just to the number(10,5,20) so i can sort them.
i need to sort it into a new text file with out using a temp txt file.
pls help me.

first read the file line by line. copy the line content into a string. now use strtok () function to seperate the copied string contends.use atoi() function to tgake the numbers out.
For sorting you can use any logic. since you have only 3 numbers you can use comparision.

can u pls write me the whole commands? cuz i dont have msdn installed... and we didnt learn how to do it yet.
and how do i sort the whole line? not just the numbers
thanks alot!

oh and another thing.
how do i refer to each line as a line? thats my main problem... thanks

oh and another thing.
how do i refer to each line as a line? thats my main problem... thanks

you can use fgets()

thats what im doing... but it doesnt work...

>thats what im doing...
Actually, you aren't doing anything at all since you haven't posted any code.

>but it doesnt work...
I keep saying this, but it keeps falling on deaf ears. "It doesn't work" is utterly, completely, totally useless! Clearly it doesn't work, otherwise you wouldn't be asking for help. But "it doesn't work" is never going to get you clear and accurate help for your problems.

thats the code i've written so far. now i need to sort it. thanks for your patience.

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

#define LEN 30
#define LENST 6

int j=0;
char *str[6];
typedef struct
{
	int id;		//מספר הזהות של הסטודנט
	char full_name[LEN];		//שם ושם משפחה
    	struct
	{
		unsigned sem_a;		//ממוצע הציונים בסמסטר ראשון
		unsigned sem_b;		//ממוצע הציונים בסמסטר שני
	}grades;					//משתנה של ציונים מטיפוס של מבנה
}Student;

Student A[]={
		{10,"   SasonSasoniBenSason   ",100,100},
		{5,"    Pinokio    ",70,90},
		{20,"       JohnRambo ",50,87},
		{3,"Elvis",3,77},
		{50," JamesBond", 17,1},
		{40,"BillGates",91,8}
	};

FILE *f;
FILE *to_f;

char first;
char second;
char c,t,g,s;
void write_2_file( char first) //writing the first file. after that im not allowed to touch A struct again.
{
	int a=1, i;
	f=fopen("one.txt", "w");
	if (f==NULL) exit(1);
	else 
	{
		printf("the file one has been opened \n\r");
		fflush(stdin);
		for(i=0;i<LENST;i++)
		{
			
				  fprintf(f, "%d %s %u %u\n\r", A[i].id, A[i].full_name, A[i].grades.sem_a, A[i].grades.sem_b);
				  
			   
			  
			
			 
		}
			  if (fclose(f)) exit(1);
			  else 
				  printf("\nthe file has been closed\n\r");
	} 

}

 void coping(char first, char second) // this function need to copy the first file to a second file sorted with no sort function. 
{

	f=fopen("one.txt", "r");
	to_f=fopen("two.txt", "w");
	if ((f== NULL) || (to_f==NULL)) exit(1);
	else
	{
	
		printf("the files have been opened\n\r");
		
		
		
		 //for (fscanf(f, "%c", &c); !feof(f); fscanf(f, "%c", &c))
		//{ 
			while (!(feof(f)))
			{
				fgets(str[j],30,f);
				//fputs(str,stdout);
			j++;
			}
			

			//fprintf(to_f, "%c", c);
			//fprintf(stdout, "%c",c);
			
		//} 
		if((fclose(to_f)) || (fclose(f))) exit(1);
		else
			printf("\n\rthe files had been closed\n\r");
	}
	
}


void main()
	{
		

		write_2_file(first);
		coping(first,second);
	}

I notice you're not allowed to use a sort function. I'm guessing that rule is specifically for qsort, but what about a function you write yourself? I get the impression that you're trying to figure out how to sort the file as you write it, which is...difficult. You'd be better off storing the records in a dynamic collection, sorting the collection, then writing the collection to your output file. That's much easier.

and how do i do that?
i cant create or use a temp file.

>i cant create or use a temp file.
You wouldn't want to unless there are so many records that you can't practically keep them all in memory. Here's a quick example using a less complicated record type. You can use it as a template:

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

int *load_file ( FILE *in, size_t *n )
{
  int *list = NULL;
  int value;

  while ( fscanf ( in, "%d", &value ) == 1 ) {
    int *save = realloc ( list, ( *n + 1 ) * sizeof *save );

    if ( save == NULL ) {
      free ( list );
      list = NULL;
      *n = 0;
      break;
    }

    list = save;
    list[*n] = value;
    ++(*n);
  }

  return list;
}

void sort_list ( int *list, size_t n )
{
  size_t i;
  size_t j;

  for ( i = 1; i < n; i++ ) {
    int save = list[i];

    for ( j = i; j >= 1 && list[j - 1] > save; j-- )
      list[j] = list[j - 1];

    list[j] = save;
  }
}

void write_file ( FILE *out, int *list, size_t n )
{
  while ( n-- > 0 )
    fprintf ( out, "%d\n", *list++ );
}

int main ( void )
{
  FILE *in = fopen ( "test.txt", "r" );
  int *list = NULL;
  size_t n = 0;

  if ( in == NULL ) {
    perror ( "Error opening file" );
    return EXIT_FAILURE;
  }

  list = load_file ( in, &n );
  sort_list ( list, n );
  write_file ( stdout, list, n );

  return EXIT_SUCCESS;
}

ive tried and tried and still nothing.
i just dont get it.
how do i copy the line in my struct and copy it as a string...

Sample code to help you

#include <stdio.h>

int main()
{
    FILE *read, *write;
    char Buffer[BUFSIZ];
    
    if( ( read = fopen("x.txt", "r") ) ==  NULL )
    {
        printf("Error: File cannot be opened\n");
        return 1;
    }
    
    if( ( write = fopen("Tempx.txt", "a") ) ==  NULL )
    {
        printf("Error: File cannot be opened\n");
        return 1;
    }    
    
    if( fgets( Buffer, BUFSIZ, read) != NULL )
        fprintf( write, "%s", Buffer );
    
    fprintf( write, "%s - %s\n", "Name", "Name2" );
    
    while( fgets( Buffer, BUFSIZ, read) != NULL )
           fprintf( write, "%s", Buffer );
    
    fclose(read);
    fclose(write);
    


    return 0;
}

ive tried and tried and still nothing.
i just dont get it.
how do i copy the line in my struct and copy it as a string...

okay, here's your structure

#
typedef struct {
    int id; 
    char full_name[LEN]; 
    struct {
        unsigned sem_a; 
        unsigned sem_b;
    }grades; 
}Student;

so you need to declare a variable of type student, then fill each of its elements. here's a very basic way that will suffice for illustration:

// declares an array of 10 students
Student myStudents[10];

// make a temp buffer long enough to read each line
char buffer[64];

// declare some temp vars to store data 
// not neccessary, but good for illustration here.
int num, first, second;
char name[32];

//  normally "buffer" would have the string read 
//  into it by fgets() ... for this example I'm just
//  using a simple strcpy( ) to illustrate
strcpy(buffer,"15 JoeSchmoe 95 87");  

// scanf expects data fields to be formatted precisely.
// format variations will cause unexpected results.
// copy the fields into temp variables
scanf(buffer,"%d %s %d %d", &num, name, &first, &second);

// write the fields into the first index of your student array structure
myStudent[0].id = num;
strcpy(myStudent[0].full_name, name);
myStudent[0].grades.sem_a = first;
myStudent[0].grades.sem_b = second;

now your "myStudent" array has its first element filled

you would of course want to use a while loop in which you would repeatedly do an fgets( ) to read the lines, along with an incremental counter to move through each index of the "myStudent" array


.

now if you want to access any element for inspection, just call it like so:

int lowestIdNum = 99;
int lowestStudentIndex= -1;

for (a = 0; a < totalNumOfStudents; a++) 
{
    if (Student[a].id <lowestIdNum )
    {
        lowestIdNum = Student[a].id;
        lowestStudentIndex= a;
    }
}

printf("Student: %s has the lowest ID number (%d)\n",
            Student[lowestStudentIndex].full_name,
            lowestIdNum );
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.