WiredWX Christian Hobby Weather Tools
Would you like to react to this message? Create an account in a few clicks or log in to continue.

WiredWX Christian Hobby Weather ToolsLog in

 


descriptionHelp: some errors for C programming. EmptyHelp: some errors for C programming.

more_horiz
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

descriptionHelp: some errors for C programming. EmptyRe: Help: some errors for C programming.

more_horiz
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.

descriptionHelp: some errors for C programming. EmptyRe: Help: some errors for C programming.

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

descriptionHelp: some errors for C programming. EmptyRe: Help: some errors for C programming.

more_horiz
What Program do you use


Try Notepad ++


~Acid

............................................................................................

Be The Best!

NotGoodSig
Help: some errors for C programming. 33trwvt
~[Filtered]~

descriptionHelp: some errors for C programming. EmptyRe: Help: some errors for C programming.

more_horiz
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


[...]

descriptionHelp: some errors for C programming. EmptyRe: Help: some errors for C programming.

more_horiz
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.

descriptionHelp: some errors for C programming. EmptyRe: Help: some errors for C programming.

more_horiz
privacy_tip Permissions in this forum:
You cannot reply to topics in this forum