This program would only print even lines data. (E.g line2,line4,line6...;skip line1,line3...)
compiler : visual studio c/c++
compiler : visual studio c/c++
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 81
void writefile(void);
void openfile(void);
void main (void)
{
writefile();
openfile();
exit(0);
}
void writefile (void)
{
FILE *log = fopen("logfile.txt", "at");
char name[16],ID[11];
if (!log) log = fopen("logfile.txt", "wt");
if (!log) {
printf("\nCould not write the following file, Check Disk!\n");
exit(1);
}//end if
printf("Please enter your name: ");
gets(name);
printf("Please enter your ID: ");
gets(ID);
fflush(stdin);
if (strlen(name) >= 16 || strlen(ID) >= 11)
{
printf("Error!\n");
exit(1);
}//end if
else
{
fprintf(log, "%-15s %-10s\n",name,ID);
}//end else
fclose(log);
}//end writefile
void openfile (void)
{
FILE *fp;
char name[16],ID[11],buf[MAX];
int i;
fp = fopen("logfile.txt", "r");
if (fp == NULL)
{
printf("\nCould not find the following file, Check Disk!\n");
exit(1);
}//end if
while ( fgets(buf, MAX, fp) != NULL )
{
fgets(buf,MAX,fp);
strncpy(name,buf,15);
name[15]=0;
sscanf(buf+16, "%s",&ID);
strupr(name);
strupr(ID);
printf("\nName: %s ID: %s\n",name,ID);
}//end while
fclose(fp);
}//end openfile