1

The following is a sample from my textbook attempting to create a sequential access file. However I continue to receive this error when I try to compilein VS2008:

MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup.

Any ideas?

Thanks in advance!

#include <stdio.h>

int main( void )
{ 
   unsigned int account; // account number
   char name[ 30 ]; // account name
   double balance; // account balance

   FILE *cfPtr; // cfPtr = clients.dat file pointer   

   // fopen opens file. Exit program if unable to create file 
   if ( ( cfPtr = fopen( "clients.dat", "w" ) ) == NULL ) {
      puts( "File could not be opened" );
   } // end if
   else { 
      puts( "Enter the account, name, and balance." );
      puts( "Enter EOF to end input." );
      printf( "%s", "? " );
      scanf( "%d%29s%lf", &account, name, &balance );

      // write account, name and balance into file with fprintf
      while ( !feof( stdin ) ) { 
         fprintf( cfPtr, "%d %s %.2f\n", account, name, balance );
         printf( "%s", "? " );
         scanf( "%d%29s%lf", &account, name, &balance );
      } // end while

      fclose( cfPtr ); // fclose closes file   
   } // end else
} // end main
Chris Dillon
  • 31
  • 1
  • 5
  • 1
    Be sure to 1) `#include ` (which has a macro for "main()"), and 2) set your makefile or MSVS project options to "console app" (not "windows app"). – FoggyDay Dec 09 '14 at 01:53
  • I should clarify that this is C, not C++ – Chris Dillon Dec 09 '14 at 01:56
  • @ChrisDillon So tag it correctly -however in this case it does not matter – mmmmmm Dec 09 '14 at 01:58
  • In this case, exactly the same for either language. – FoggyDay Dec 09 '14 at 01:58
  • Also this is with VS2008. Subsystem is set to console. – Chris Dillon Dec 09 '14 at 01:59
  • Sorry tag changed. The above does not correct the problem. – Chris Dillon Dec 09 '14 at 02:01
  • if the code used do { ... } while(...); rather than while(...) {...}, then the code would not have to be repeated. – user3629249 Dec 09 '14 at 04:59
  • regarding the scanf() calls, 1) the code needs to check the returned value to assure all 3 conversions were successful 2) the format sting should contain a leading ' ' so that newline and other white space are properly consumed/ignored. 3) the format string should have a space between each input conversion 4) the instructions to the user should say to separate each value by a space. – user3629249 Dec 09 '14 at 05:02
  • both the user and you want to know why the output file could not be opened for write. Therefore, instead of: puts( "File could not be opened" ); use perror("fopen failed for write:); – user3629249 Dec 09 '14 at 05:04
  • @FoggyDay: Inclusion of windows.h is certainly not required. – James McNellis Dec 09 '14 at 07:06
  • @ChrisDillon: How did you create your project? This program, as-is, should compile and link successfully. – James McNellis Dec 09 '14 at 07:07

3 Answers3

1

OK - your program should compile and link without the error, without you having to do anything special. In other words, you shouldn't have to "#include windows.h" (although it couldn't hurt) and you shouldn't have to explicitly use "tmain()" (even though that's the actual entry, behind the MSVC macros).

It should "just work".

SUGGESTIONS:

1) Verify that you can compile, link and execute ANY C program. For example:

a) Type in this program: notepad tmp.c

#include <stdio.h>

int main() {
  printf ("Hello world!\n");
  return 0;
}

b) Compile: from the MSVS IDE, or from the command line (you can use the MSVS .bat file "vcvars32.bat" to set your command prompt environment:

d:\temp>cl tmp.c
Microsoft (R) C/C++ Optimizing Compiler Version 17.00.50727.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

tmp.c
Microsoft (R) Incremental Linker Version 11.00.50727.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:tmp.exe
tmp.obj

c: Execute your program: D:\temp>tmp Hello world!

2) I have a different, newer version of MSVS ... but it should absolutely work the same for your MSVS 2008.

3) If your "hello world" (new MSVS project and/or command line build) fails with the same link error, you might want to consider re-installing MSVS. Consider getting a newer version (e.g. Visual Studio 2013 Community), if at all possible:

http://www.visualstudio.com/

MSVS 2012 Express

4) If your "hello world" works (and it should), consider creating a new project from scratch, then copying your files into the new project.

This complete example compiles and runs OK for me:

#include <stdio.h>
#include <string.h>

#define MAX_NAME 30
#define MAX_LINE 80
#define CLIENTS_FILE "clients.dat"

int main () { 
   char line[MAX_LINE], acct_name[MAX_NAME];
   unsigned int acct_no;
   double acct_balance;
   int iret;

   /* Open file.  Print error and return non-zero status if error */
   FILE *fp = fopen(CLIENTS_FILE, "w" );
   if (!fp) {
      perror ("clients file open error");
      return 1;
   } 

   do {
     /* Get next record */
     printf ("Enter the account, name, and balance. \"q\" to exit.\n");
     fgets (line, MAX_LINE, stdin);

     /* Check for end of data */
     if (line[0] == 'q')
       break;

     /* Parse data */
     if ((iret = sscanf(line, "%d %s %lf", &acct_no, acct_name, &acct_balance )) == 3) {
       /* Write to file */
       fprintf(fp, "%d %s %lf\n", acct_no, acct_name, acct_balance );
     } else {
       /* Print warning */
       printf ("Error: unable to parse input: #/items parsed = %d, line = %s", iret, line);
     }

    } while (line[0] != 'q');

    /* Close file and return "OK" status */
    fclose (fp);
    printf ("Done.\n");
    return 0;
}
FoggyDay
  • 11,962
  • 4
  • 34
  • 48
1

I was working with Visual Studio 2017 and received that error. For me the solution was:

Main Menu -> Project -> Projectname - properties.

Then:

Linker->System and on right side for "SubSystem" I chose "Windows". My code got compiled perfectly afterwards.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Sandra th
  • 11
  • 1
0

Double check in vs project the source files contain the source file containing your main function from the project. For me, a bug in my cmake file lead to the generation of vs project files missing my source files which resulted in this error.

TheoretiCAL
  • 19,461
  • 8
  • 43
  • 65