0

I have been trying to compile a C program that receives data from a .data file.Unfortunately,I always get the following message: input.dat.2:1:error: expected identifier or "(" before numeric constant I am using the 2014 version of Ubuntu Linux.Actually,this is my first program at Linux environment.It is homework for my university.The same program worked for windows dev-C++! This is the program:

#include <stdio.h>
#include "input.dat"
#define NUM_NODES                          100
#define NONE                               9999
#include<csdl_queue.h>
#include<cdsl_deque.h>
#include<cdsl_dyn_array.h>

struct _NODE
{
  int iDist;
  int iPrev;
};
typedef struct _NODE NODE;

struct _QITEM
{
  int iNode;
  int iDist;
  int iPrev;
  struct _QITEM *qNext;
};
typedef struct _QITEM QITEM;

QITEM *qHead = NULL;

int AdjMatrix[NUM_NODES][NUM_NODES];

int g_qCount = 0;
NODE rgnNodes[NUM_NODES];
int ch;
int iPrev, iNode;
int i, iCost, iDist;


void print_path (NODE *rgnNodes, int chNode)
{
  if (rgnNodes[chNode].iPrev != NONE)
    {
      print_path(rgnNodes, rgnNodes[chNode].iPrev);
    }
  printf (" %d", chNode);
  fflush(stdout);
}


void enqueue (int iNode, int iDist, int iPrev)
{
  QITEM *qNew = (QITEM *) malloc(sizeof(QITEM));
  QITEM *qLast = qHead;

  if (!qNew) 
    {
      fprintf(stderr, "Out of memory.\n");
      exit(1);
    }
  qNew->iNode = iNode;
  qNew->iDist = iDist;
  qNew->iPrev = iPrev;
  qNew->qNext = NULL;

  if (!qLast) 
    {
      qHead = qNew;
    }
  else
    {
      while (qLast->qNext) qLast = qLast->qNext;
      qLast->qNext = qNew;
    }
  g_qCount++;

}


void dequeue (int *piNode, int *piDist, int *piPrev)
{
  QITEM *qKill = qHead;

  if (qHead)
    {

      *piNode = qHead->iNode;
      *piDist = qHead->iDist;
      *piPrev = qHead->iPrev;
      qHead = qHead->qNext;
      free(qKill);
      g_qCount--;
    }
}


int qcount (void)
{
  return(g_qCount);
}

int dijkstra(int chStart, int chEnd) 
{



  for (ch = 0; ch < NUM_NODES; ch++)
    {
      rgnNodes[ch].iDist = NONE;
      rgnNodes[ch].iPrev = NONE;
    }

  if (chStart == chEnd) 
    {
      printf("Shortest path is 0 in cost. Just stay where you are.\n");
    }
  else
    {
      rgnNodes[chStart].iDist = 0;
      rgnNodes[chStart].iPrev = NONE;

      enqueue (chStart, 0, NONE);

     while (qcount() > 0)
    {
      dequeue (&iNode, &iDist, &iPrev);
      for (i = 0; i < NUM_NODES; i++)
        {
          if ((iCost = AdjMatrix[iNode][i]) != NONE)
        {
          if ((NONE == rgnNodes[i].iDist) || 
              (rgnNodes[i].iDist > (iCost + iDist)))
            {
              rgnNodes[i].iDist = iDist + iCost;
              rgnNodes[i].iPrev = iNode;
              enqueue (i, iDist + iCost, iNode);
            }
        }
        }
    }

      printf("Shortest path is %d in cost. ", rgnNodes[chEnd].iDist);
      printf("Path is: ");
      print_path(rgnNodes, chEnd);
      printf("\n");
    }
}

int main() {
  int i,j,k;
  FILE *fp;

  fp=fopen("input.dat","r");
  /* open the adjacency matrix file */
  /* make a fully connected matrix */
  for (i=0;i<NUM_NODES;i++) {
    for (j=0;j<NUM_NODES;j++) {
      /* make it more sparce */
      fscanf(fp,"%d",&k);
    AdjMatrix[i][j]= k;
    }
  }

  /* finds 10 shortest paths between nodes */
  for (i=0,j=NUM_NODES/2;i<20;i++,j++) {
            j=j%NUM_NODES;
      dijkstra(i,j);
  }
  fclose(fp);
  exit(0);


}

The program probably works,as it was granted to us by the teachers.The input.dat is composed only of numbers.I guess that the Ububtu system is somehow responsible.Why does the compilation proccess fail?I would be gratefull if anyone could help me!

2
  • Well,I just removed the second line and it worked!Thank you for your time!!Well,I should had noticed it,since I don't have the second line at windows.I added it at linux because I always get lost with directories.That line costed me more that 6 hours of effort..Thanks! Jun 3, 2015 at 14:42
  • 1st I don't see what you comment under your own post if I didn't write there. 2nd please mark the answer as accepted if it solved your problem.
    – s3lph
    Jun 3, 2015 at 15:23

1 Answer 1

2

Remove the second line from your program:

#include "input.dat"

This line will cause the input file to be read by the compiler. The way you explained it, the input file is no C code, and therefore will not compile.

The input file, however, is not to be included into your program, but to be read from the file system at runtime.


If you say the same program worked on Windows, do you mean that the exact same code worked on Windows? That would really surprise me, as only numbers isn't C, no matter which platform you are on.

Not the answer you're looking for? Browse other questions tagged .