Like Tree1Likes
  • 1 Post By memcpy

Syntex error at the end of input error! help!

This is a discussion on Syntex error at the end of input error! help! within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #define N 3 #define space 1 #define wpl 9 #define maxw 50 int fsize(FILE *fp){ int p=ftell(fp); ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    3

    text file modifying code, please help

    Code:
    #include <stdio.h>
    #define N 3
    #define space 1
    #define wpl 9
    #define maxw 50
    
    int fsize(FILE *fp){
        int p=ftell(fp);
        fseek(fp, 0L, SEEK_END);
        int s=ftell(fp);
        fseek(fp,p,SEEK_SET); //go back to where we were
        return s;
    }
    
    
    int main ( int argc, char *argv[] )
    {
        if ( argc != 2 )
        {
            printf( "usage: %s filename", argv[0] );
        }
        else
        {
            FILE *file = fopen( argv[1], "r" );
            if ( file == 0 )
            {
                printf( "Could not open file\n" );
            }
            else
            {
                int sz = fsize(file);
                char *buf = malloc(sz);
                fscanf (file,"%s", buf);
                int i;
                for (i =0 ; i<sz; i++){           
                    int a;
                    int b = 1;
                    int p = 1;
                    char s = (*(buf+i));
                    if (a == N && s != ' '){
                            i = i++;
                    }
                    else if (s != ' '){
                            printf ("%s",s);
                            a++;
                    }
                    else {
                            b++;
                            p++;
                            int c;
                            for (c = 0; c < space; c++){
                                    printf (" ");
                            }
                            a=0;                            
                            if (b == (wpl)){
                                    printf ("\n");
                            }
                            if (p == (maxw)){
                            break;
                            }
                    }
            }
                fclose(file);
        }
    }
    }
    so this code is suppose to be in command line taking a text file as input
    if in the text file it says
    hello world this is cool

    then as in the preprocessing definition defined
    it would display 3 letters of each word, 1 space between each word, 9 line per word, max output word is 50
    as result it would display

    hel wor thi is coo

    but somehow its not working...please help

    its giving me a segamentation fault core dump
    Last edited by Jackie Chen; 03-19-2012 at 09:24 PM.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > i did check the braces 3 times..
    Nope! Check again! You need another one to balance out main's starting one.

    Also, #include <stdlib.h> if you want to use malloc. And free memory when you're done using it.

    Code:
    printf ("&s",s);
    Did you mean to use "%"?

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    3

    thanks

    sorry yeah i got that one fixed, but the program isn't working right now

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Don't spam! One topic is enough > text file modifying program code, please help

    Why not use fgets/fread instead of fscanf? Fscanf (if I'm correct) will stop if it reaches a space.

    Code:
    i = i++;
    Textbook example of undefined behavior. Replace it with:
    Code:
    i++;
    Finally, you're still not free()'ing memory that you allocate.
    laserlight likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 11-30-2011, 11:55 AM
  2. fscanf syntex
    By calc in forum C Programming
    Replies: 2
    Last Post: 07-12-2009, 09:50 AM
  3. Input Error
    By manzoor in forum C++ Programming
    Replies: 17
    Last Post: 11-12-2007, 08:28 AM
  4. INPUT error
    By Tanner.B in forum C++ Programming
    Replies: 9
    Last Post: 03-29-2007, 06:13 PM
  5. Input error
    By mfantastique in forum C Programming
    Replies: 9
    Last Post: 11-14-2004, 01:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21