i keep tried many time and the program could not be scan and no output displayed and some illegal character.... pls help or give some guide .... thanks you....

here is my code

#include<stdio.h>
#include<stdlib.h>
#define NO_OF_WEEKS 14
#define NO_OF_STUDS 5
void looping();
void List_Student_Details();

typedef struct
{
	int year;
	char campus;
	char school;
	char level;
	char sn[6];
}regNUM;
regNUM regno;

typedef struct
{
	int studno;
	regNUM regno;
	char surname[5];
	char givenname[8];
	char gender;
	char fathername[7];
	char addr1[12];
	char addr2[12];
	char addr3[6];
}Student;

Student details[NO_OF_STUDS];

int main()
{
	looping();
}

void looping()
{
	int ans=0;
		printf("Please Choose an option on this CLASS ATTENDANCE SYSTEM.\n");
		printf("[1]Display the List of Student Details.\n");
		printf("[6]Display the Poor Attendance list.\n");
		printf("[7]Exit.\n");
		printf("Your Choice is ...... ");
		scanf("%d", &ans);

		if(ans==1)
		{
			List_Student_Details();
		}
		else
		{
			printf("Bye bye.\n");
		}
}

void List_Student_Details()
{
	int n=0;
	FILE *stud_d;
	stud_d=fopen("StudentDetails.txt","r");
	printf("FUCKER");
	while(fscanf(stud_d,"%d%d%c%c%c%[^|]|%[^|]|%[^|]|%c|%[^|]|%[^|]|%[^|]|%[^\n]\n",
		&details[n].studno,
		&details[n].regno.year,
		&details[n].regno.campus,
		&details[n].regno.level,
		details[n].regno.sn,
		details[n].surname,
		details[n].givenname,
		&details[n].gender,
		details[n].fathername,
		details[n].addr1,
		details[n].addr2,
		details[n].addr3)!=EOF)
	{
		printf("%d%d%c%c%c%s", details[n].studno, details[n].regno.year, details[n].regno.campus, details[n].regno.school, details[n].regno.level, details[n].regno.sn);
		n++;
	}

	fclose(stud_d);
}

my txt file content is like below...

1|10WAD03824|Ang|San Nin|M|Louis|87 jln bola|taman raya|KL
2|10WAD07575|Gan|Kel Lee|F|Andy|lorong 65|Taman PJ|perak
3|10WAD06321|Chan|Mel vin|M|Ben|jln 218|taman KP|KL
4|10WAD07819|Lau|Mei Lee|F|Christ|88 jln ipoh|Taman petro|kedah
5|10WAD05208|Khan|Ken See|M|jesus|77 jln ultra|taman bb|KL

i realize that the value stored in details[n].school is null or spacebar , howto deal with problem to avoid fscanf the %c(\0) from text file into my program?

#include<stdio.h>
#include<stdlib.h>
#define NO_OF_WEEKS 14
#define NO_OF_STUDS 5
void looping();
void List_Student_Details();

typedef struct
{
	int year;
	char campus;
	char school;
	char level;
	char sn[6];
}regNUM;
regNUM regno;

typedef struct
{
	int studno;
	regNUM regno;
	char surname[5];
	char givenname[8];
	char gender;
	char fathername[7];
	char addr1[12];
	char addr2[12];
	char addr3[6];
}Student;

Student details[NO_OF_STUDS];

int main()
{
	looping();
}

void looping()
{
	int ans=0;
		printf("Please Choose an option on this CLASS ATTENDANCE SYSTEM.\n");
		printf("[1]Display the List of Student Details.\n");
		printf("[6]Display the Poor Attendance list.\n");
		printf("[7]Exit.\n");
		printf("Your Choice is ...... ");
		scanf("%d", &ans);

		if(ans==1)
		{
			List_Student_Details();
		}
		else
		{
			printf("Bye bye.\n");
		}
}

void List_Student_Details()
{
	int n=0;
	FILE *stud_d;
	stud_d=fopen("StudentDetails.txt","r");
	printf("\nREPORT before fscanf\n");
	while(fscanf(stud_d,"%d|%d%c%c%c%[^|]|%[^|]|%[^|]|%c|%[^|]|%[^|]|%[^|]|%[^\n]\n",
		&details[n].studno,
		&details[n].regno.year,
		&details[n].regno.campus,
		&details[n].regno.level,
		details[n].regno.sn,
		details[n].surname,
		details[n].givenname,
		&details[n].gender,
		details[n].fathername,
		details[n].addr1,
		details[n].addr2,
		details[n].addr3)!=EOF)
	{
		printf("%d\n%d\n%c\n%c\n%c\n", details[n].studno, details[n].regno.year,details[n].regno.campus,details[n].regno.school,details[n].regno.level);
		n++;
	}
	fclose(stud_d);
	looping();
}

Edited 6 Years Ago by aznelite89: n/a

fscanf stops reading a string when it finds a whitespace, so you should ensure you dont have spaces in the string or use another character instead (_).

void List_Student_Details()
{
    int n=0; // <-----------------
   (...)
        &details[n].studno,
        &details[n].regno.year,
        &details[n].regno.campus,
        &details[n].regno.level,
        details[n].regno.sn,
        details[n].surname,
        details[n].givenname,
        &details[n].gender,
        details[n].fathername,
        details[n].addr1,
        details[n].addr2,
        details[n].addr3)!=EOF)
(...)
        n++;
    }

    fclose(stud_d);
}

This n will reset whenever the function is called because it is defined inside it. It should be defined as global (outside any functions).

The following part:

int main()
{
	looping();
}

could have been done like this:

int main()
{
   bool loop = true;
   while(loop){
      //stuff inside looping()
      //set loop to false to break
   }
}

STILL WRITTING.. (WILL EDIT)

Edited 6 Years Ago by darkbreaker: n/a

(I didn't know we have a timer before you can't edit again, so I'm resuming the post here)
RESUMING:


The following example covers your main problem, test it and implement in your program after.

#include<stdio.h>

int main(void){
    FILE *hfile;
    char buffer[150],p1[10],p2[10],p3[10],p4[10];
    hfile = fopen("example.txt","r");
    while(fgets(buffer,150,hfile))
    {
        sscanf(buffer,"%[^|]|%[^|]|%[^|]|%s",p1,p2,p3,p4);
        printf("var1(%s) var2(%s) var3(%s) var4(%s)\n",p1,p2,p3,p4);
        
    }
    fclose(hfile);
    getchar();
    return 0
}

Having in mind that example.txt is as follows:

ABC1|ABC2|ABC3|ABC4
DEF1|DEF2|DEF3|DEF4
GHI1|GHI2|GHI3|GHI4

The output you get is this:

var1(ABC1) var2(ABC2) var3(ABC3) var4(ABC4)
var1(DEF1) var2(DEF2) var3(DEF3) var4(DEF4)
var1(GHI1) var2(GHI2) var3(GHI3) var4(GHI4)

--

sscanf(buffer,"%[^|]|%[^|]|%[^|]|%s",p1,p2,p3,p4);

Let's understand what the above means.

sscanf reads a string according to the format specified then write into the variables specified after the format.

%[^|] means read all characters that are not '|', this means that it will read the start of the string and will stop when it finds a '|' character. The '|' after it will be read but sscanf will ignore it because it doesn't have a % before. We want all text after the last '|' so we just use %s.

Edited 6 Years Ago by darkbreaker: n/a

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