GeekPolice
Welcome to GeekPolice.net!

GeekPolice is a website which provides free Computer Technical Support & Virus/Spyware Removal to our members.

You are currently viewing the forum as "Guest" which doesn't give you the same privilege as members to ask questions or post comments.

Click the Register button below to unlock the limitations of this website and start asking questions to discover new computer knowledge now!

Help: some errors for C programming.

Post new topic   Reply to topic

View previous topic View next topic Go down

Help: some errors for C programming.

Post by pokkax on Thu Feb 04, 2010 9:07 am

This program would only print even lines data. (E.g line2,line4,line6...;skip line1,line3...)

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
Unborn

Posts: 1
Joined: 2010-02-04
Operating System: Windows 7

View user profile

Back to top Go down

Re: Help: some errors for C programming.

Post by DragonMaster Jay on Fri Feb 05, 2010 8:16 pm

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.

DragonMaster Jay

Moderator | Tech Officer
Moderator | Tech Officer

Posts: 5503
Joined: 2009-09-06
Operating System: Windows 7 Ultimate 32-Bit

View user profile http://www.twitter.com/dragonmasterjay

Back to top Go down

Re: Help: some errors for C programming.

Post by devesh2010 on Sun Feb 14, 2010 10:09 am

at is not a file open format use a+ or r+
instead

devesh2010

Unborn
Unborn

Posts: 1
Joined: 2010-02-14
Operating System: windows xp

View user profile

Back to top Go down

Re: Help: some errors for C programming.

Post by DoctorAcid on Sun Feb 14, 2010 2:24 pm

What Program do you use


Try Notepad ++


~Acid


Be The Best!

NotGoodSig

~[Filtered]~

DoctorAcid

Rookie Surfer
Rookie Surfer

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

View user profile http://twitter.com/wzZRyaNZzw

Back to top Go down

Re: Help: some errors for C programming.

Post by GeorgeDorn on Fri Feb 26, 2010 11:00 pm

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

Unborn
Unborn

Posts: 2
Joined: 2010-02-27
Operating System: Windows 7 / Debian

View user profile

Back to top Go down

Re: Help: some errors for C programming.

Post by Programmer on Thu Mar 11, 2010 8:12 pm

Also when declaring a method, if you say it is a void,

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
Newbie Surfer

Posts: 17
Joined: 2009-11-04
Operating System: Windows 7 Ultimate, Ubuntu 9.04, Windows XP Profession SP2

View user profile

Back to top Go down

View previous topic View next topic Back to top


Permissions of this forum:
You cannot reply to topics in this forum