
November 16th, 2012, 10:23 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 1
Time spent in forums: 2 h 56 sec
Reputation Power: 0
|
|
Getting a syntax error near unexpected token `newline' in C
Okay, so I went to run my program and I started getting,
-bash: syntax error near unexpected token `newline'.
I'm not exactly sure why this is occurring.
Also, I apologize if I put the code in wrong. It's too long to just copy and paste directly, so hopefully it copied into the "code" tab correctly.
Also, I'm not sure if it matters, but I'm using notepad++ and compiling with cygwin.
Thanks!
Code:
#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX_LINE_LEN 1024 #define MAX(a,b) (((a) > (b)) ? (a) : (b)) #define LINE_WIDTH 70 #define WS " \":-(){}[]<>/\\=+\t" int main(int argc, char *argv[]) { FILE *fp; char line[MAX_LINE_LEN],*s, **words, fmt[8], *p; size_t numWords = 0, maxWordLen = 0, i = 0, j = 0, len, wordsPerLine; if ((argc < 2) || ((fp = fopen(argv[1],"r")) == NULL)) { printf("\nusage : %s <filename>\n",argv[0]); return -1; } /* count words */ while (fgets(line, MAX_LINE_LEN, fp) != NULL) { for (s = strtok(line,WS); s != NULL; s = strtok(NULL,WS)) { ++numWords; } } /* store words, and find length of longest word */ fseek(fp, 0, SEEK_SET); words = calloc(sizeof(char *), numWords); while (fgets(line, MAX_LINE_LEN, fp) != NULL) { if ((p = strchr(line,'\n')) != NULL) *p = '\0'; if ((p = strchr(line,'\r')) != NULL) *p = '\0'; for (s = strtok(line,WS); s != NULL; s = strtok(NULL,WS)) { words[i] = calloc(sizeof(char), (len = strlen(s)) + 1); strcpy(words[i++], s); maxWordLen = MAX(maxWordLen, len); } } fclose(fp); /* display */ wordsPerLine = LINE_WIDTH / maxWordLen; sprintf(fmt, "%%-%ds ", maxWordLen); for (i = 0; i < numWords; i++) { printf(fmt, words[i]); if ((++j % wordsPerLine) == 0) puts(""); } puts(""); /* when done with words[][], free allocated memory */ return 0; }
|