GeekPolice
Welcome to GeekPolice.net!

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

You are viewing the forum as a "Guest" which doesn't give you member privileges to ask questions or post comments.

Take 30 seconds to register below and unlock the limitations of this website to discover new computer knowledge!

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 04 Feb 2010, 6: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 05 Feb 2010, 5: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
Global Moderator/Malware Expert


Note: replies from me are slow on weekends.

Donate! - GP on Facebook! - Twitter!

DragonMaster Jay

Super Moderator | Tech Officer
Super Moderator | Tech Officer

Posts: 9223
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 14 Feb 2010, 7: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 14 Feb 2010, 11:24 am

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 26 Feb 2010, 8: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

Newbie Surfer
Newbie Surfer

Posts: 13
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 11 Mar 2010, 5: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: 38
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