Help: some errors for C programming.
Page 1 of 1 • Share •
Help: some errors for C programming.
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

pokkax
Unborn
- Posts: 1
Joined: 2010-02-04
Operating System: Windows 7
Re: Help: some errors for C programming.
Search online for the diagnostic tool for the version of Visual Studio you run.
You might find this to be successful, otherwise try the debugger.
You might find this to be successful, otherwise try the debugger.
DragonMaster Jay
Global Moderator/Malware Expert

Note: replies from me are slow on weekends.
Donate! - GP on Facebook! - Twitter!

DragonMaster Jay
Super Moderator | Tech Officer
- Posts: 9223
Joined: 2009-09-06
Operating System: Windows 7 Ultimate 32-Bit

Re: Help: some errors for C programming.
at is not a file open format use a+ or r+
instead
instead

devesh2010
Unborn
- Posts: 1
Joined: 2010-02-14
Operating System: windows xp
Re: Help: some errors for C programming.
What Program do you use
Try Notepad ++
~Acid
Try Notepad ++
~Acid
Be The Best!
NotGoodSig

NotGoodSig

~[Filtered]~

DoctorAcid
Rookie Surfer
- Posts: 92
Joined: 2010-02-12
Operating System: Windows XP, Windows 7 Ultimate

Re: Help: some errors for C programming.
This is because you read a line with fgets and then throw that line away in the subsequent call to fgets. Remove the fgets inside your while loop and it will probably work.
- Code:
[...]
while ( fgets(buf, MAX, fp) != NULL )
{
// fgets(buf,MAX,fp); <--- REMOVE ME
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
[...]

GeorgeDorn
Newbie Surfer
- Posts: 13
Joined: 2010-02-27
Operating System: Windows 7 / Debian
Re: Help: some errors for C programming.
Also when declaring a method, if you say it is a void,
You do not need to put void in the parameters as it is already going to return nothing and its not going to take any parameters either so don't put anything.
Hope this helped.
- Code:
void WriteFile(void)
You do not need to put void in the parameters as it is already going to return nothing and its not going to take any parameters either so don't put anything.
- Code:
void WriteFile()
Hope this helped.
Programmer
Newbie Surfer
- Posts: 38
Joined: 2009-11-04
Operating System: Windows 7 Ultimate, Ubuntu 9.04, Windows XP Profession SP2
Permissions of this forum:
You cannot reply to topics in this forum













by 