Hello dear fellow coders.
As a part of a project I have obtained from a friend some code to read from a file. It was written in C on linux and is confirmed working there. I have got the header and source, but trying to compile on my windows computer running Visual Studio 2008 Pro reports errors.
I should say that I have only supplied the following code "up till" the point where I get an error message. If the entire code is needed, it will be supplied (in total approx 600 lines)
First of all the .h file
Code:
#ifndef __GAPREAD_H
#define __GAPREAD_H
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*-----------------------------------------------------------------------------*/
int GAPopenfile( char* fname, char** probname );
/*------------------------------------------------------------------------------
PURPOSE:
Opens a file named fname as a "GAPFILE" for reading and returns the "problem name",
that is the filename without path and extension.
PARAMETERS:
- fname : pointer to an array of chars containing the name of the data file
- probname: if probname is not NULL on input, the problem's name is
returned on output in *probname.
RETURN VALUE: 0 on success, 1 if file unable to open file, 2 if error
occurred while reading number of instances
-------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
And the the C code until the point of error
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "gapread.h"
static FILE* GAPfile=NULL; /* Handle to the data file */
static int GAPnuminst=0; /* number of problem instances in input file */
static int GAPcurinst=0; /* current problem instance */
static char* GAPpname=NULL; /* problem name */
static double** GAPprofit=NULL; /* GAPprofit[i][j] is the profit of assigning
job j to agent i */
static int** GAPweight=NULL; /* GAPweight[i][j] is resource requirement
of assigning job j to agent i */
static int* GAPcap=NULL; /* GAPcap[i] is agent i's capacity */
static double* GAPpptr=NULL; /* pointer to profits of assigning jobs stored */
/* as one array */
static int* GAPwptr=NULL; /* pointer to weights of assigning jobs stored */
/* as one array */
/*-----------------------------------------------------------------------------*/
int GAPopenfile( char* fname, char** probname ) {
/*
Description:
------------
Opens file named fname as a "GAPFILE" for reading, records the number of
problem instances contained in the data file and returns the "problem name",
that is the filename without path and extension.
Parameters:
------------
- fname : pointer to an array of chars containing the name of the data file
- probname: pointer to pointer to an array of chars that on output contains
the problem name
Return value:
------------
- 0 on success
- 1 if file fname could not be found
- 2 if error on reading number of instances has occurred
Scope: Export
-------------
*/
char *cptr = strrchr ( fname, (int)'/' );
char *aline=NULL, *token=NULL;
const char delim[] = " ";
int num, len;
/* Open the data file */
GAPnuminst = 0;
GAPcurinst = 0;
GAPpname = NULL;
GAPprofit = NULL;
GAPweight = NULL;
GAPcap = NULL;
if ( probname ) *probname = NULL;
GAPfile = fopen( fname, "r" );
if ( GAPfile == NULL ) return( 1 );
/* Get the problem name from the file name */
if ( cptr == NULL ) cptr = fname; else cptr++;
GAPpname = strdup( cptr );
/* Check if file has info about number of problem instances in data file */
aline = (char*) malloc( 1000 );
num = getline(&aline,&len,GAPfile);
token = strtok( aline, delim );
num = 0;
while ( token != NULL ) {
num++;
token = strtok ( NULL, delim );
}
fclose( GAPfile );
free(aline);
/* Reopen file and read number of instances */
GAPfile = fopen( fname, "r" );
GAPnuminst = 1;
if ( num == 1 ) {
if ( fscanf(GAPfile,"%d\n",&GAPnuminst) != 1 ) return( 2 );
}
if ( probname ) *probname = GAPpname;
return( 0 );
}
I get the error
Code:
Compiling is ok
Linking...
1>gapread.obj : error LNK2019: unresolved external symbol _getline referenced in function _GAPopenfile
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>C:\Documents and Settings\tuec\My Documents\Reader\Debug\Reader.exe : fatal error LNK1120: 2 unresolved externals
From the 3 hours I have spent googling it could have something to do with the getline not being included/defined. However since it is compiling and running on linux I speculate that it has to do with something else. From what I have read the end line "symbol" for windows is \r\n but for linux it is \n. However I cannot see how this should make the linking part failing.....
Hopefully I have just done something stupid and the problem is easily solved.
Kindest regards
Lagrange