Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct info_node
{
char fname[20] ;
char lname[20] ;
int rank ;
float score ;
struct info_node * next ;
} ;
char *mangle_filename(const char *in)
{
size_t len = strlen(in);
char *out, *end, *pos;
out = malloc(len + 6);
if(!out)
return 0;
pos = strrchr(in, '.');
strncpy(out, in, pos-in);
strcat(out, "_data");
strcat(out, pos);
return out;
}
void copied_and_pasted(const char *ifilename1)
{
FILE* infile ;
FILE* infile2 ;
struct info_node* link1, * link2 ;
struct info_node* top, * top2 ;
struct info_node* move_pntr = NULL, * move_pntr2 = NULL ;
struct info_node* pre_pntr ;
struct info_node* current_pntr ;
int num = 0 ;
char *ifilename2 = mangle_filename(ifilename1);
if(!ifilename2)
{
fputs("Uh oh... out of memory.", stderr);
return;
}
infile = fopen(ifilename1, "r") ;
infile2 = fopen(ifilename2, "r") ;
free(ofilename);
link1 = (info_node *)malloc(sizeof(info_node) ) ;
link2 = (info_node *)malloc(sizeof(info_node) ) ;
fscanf( infile, "%s%s%d%f", link1->fname , link1->lname, &link1->rank, &link1->score ) ;
fscanf( infile2, "%s%s%d%f", link2->fname , link2->lname, &link2->rank, &link2->score ) ;
link1->next = NULL ;
link2->next = NULL ;
top = link1 ;
top2 = link2 ;
while( !feof(infile) )
{
link1 = (info_node *)malloc(sizeof(info_node) ) ;
fscanf( infile, "%s%s%d%f", link1->fname , link1->lname, &link1->rank, &link1->score ) ;
link1->next = NULL ;
//pre_pntr = top ;
//current_pntr = top ;
link1->next = top ;
top = link1 ; //save block of mem
}
while(!feof(infile2) )
{
link2 = (info_node *)malloc(sizeof(info_node) ) ;
fscanf( infile2, "%s%s%d%f", link2->fname , link2->lname, &link2->rank, &link2->score ) ;
link2->next = NULL ;
link2->next = top2 ;
top2 = link2 ; //save block of mem
}
fclose(infile);
fclose(infile2);
}
int main()
{
copied_and_pasted("p3data.txt");
return (0) ;
} //end of func main()