C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 16th, 2012, 11:23 PM
itzvital itzvital is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 1 itzvital User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 56 sec
Reputation Power: 0
Wink 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; }

Reply With Quote
  #2  
Old November 29th, 2012, 08:14 AM
leonvictor2012 leonvictor2012 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 12 leonvictor2012 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 27 m 40 sec
Reputation Power: 0
Newline in c

I am getting this error while trying your given code so may be its an issue of your notepad++.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Simple program to take in a file in C


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap