This is the error I have been receiving when trying to compile my code:

blade71(45)% gcc -o trim trim.c
Undefined first referenced
symbol in file
getline /var/tmp//ccYdyoM3.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

My source is only a rough draft (Done in roughly 20 minutes). I have not yet began to break up the code into functions although I plan on line counting, whitespace counting, and trimming the whitespace all in separate functions. The problem is from K&R Programming C. The program is supposed to remove all excess whitespace from a program so when you do an octal dump there are no excess whitespace characters. Please don't tell me to use a library outside of stdio.h or how to do my program unless you see something dramatically wrong. (aside from my lack of functions and java like syntax)

#include <stdio.h>

/* This program will remove all trailing white space from an input file.
   Using a loop containing several if statements to check for the ASCII
   value of any white space generating characters; the program will
   individually access each character until it reaches the EOF.        */

main()
{
        int c, newline, line, bytes_read;
        int trim_count;
        int nbytes = 100;
        char *my_string;
        my_string = (char *) malloc (nbytes + 1);
        bytes_read = getline (&my_string, nbytes, stdin);

        while ((c = getchar())!= EOF)
                {
                        if (c == '\n' || c == ' ' || c == '\t')
                        {
                                newline++;
                        }
                }
        while (bytes_read !=0)
                {
                        line++;
                }
        trim_count = line - newline;
        while ((c = getchar())!= EOF)
                {
                        if (c == '\n' || c == ' ' || c == '\t')
                        {
                                trim_count--;
                                putchar(c);
                        }
                        else if (trim_count <= 0)
                        {
                                continue; /* Had to be used because */
                        }                /* of getchar's 1 argument */
                        else
                        {
                                putchar(c);
                                continue;
                        }
                }
}

Recommended Answers

All 20 Replies

Well, you call getline, but as far as the compiler is concerned, the only place getline exists is where you called it... Is that dramatically wrong enough for you?

Edit:

By the way, you really ought to turn the warnings for your compiler on. They are there to be used, and they are tremendously helpful. In your example, you would want to do:

gcc -Wall -Wextra -o trim trim.c

If you use the -Wall & -Wextra switches, you will get a lot more helpful warning & error messages (including line numbers where the problems can be found). Give it a try - you will see that you have a lot more wrong with your code than you initially thought.

Dumb question, did you include stdio.h

#include <iostream>

#include <iostream>

Can't use that in C programs.

#include <iostream>

Why would you want to include that header file in a C program? Interestingly enough, that would introduce another error to the compiler output.

Sorry, I could swear I was in a C++ forum. Clearly is time for me to take a break, I can't think straight no more :/

commented: :) +36

I actually found the problem....google "getline solaris". Because I am on a Sunblade(Solaris) I cannot use getln, fgets, or basically anything other than string.h which is not supposed to be used for this program. My next way of attempting to simply solve the program will be to copy every individual character into an array using getchar and then begin copying the array into a second array that has an if statement that checks if it is a whitespace character duplicate before copying. Can anyone think of any better way to do this that does not use any external libraries or getline, getln, fgets?....basically just getchar() and putchar()

I actually found the problem....google "getline solaris". Because I am on a Sunblade(Solaris) I cannot use getln, fgets, or basically anything other than string.h which is not supposed to be used for this program. My next way of attempting to simply solve the program will be to copy every individual character into an array using getchar and then begin copying the array into a second array that has an if statement that checks if it is a whitespace character duplicate before copying. Can anyone think of any better way to do this that does not use any external libraries or getline, getln, fgets?....basically just getchar() and putchar()

I'm confused by this???Why can't you use C standard libraries? This is a C program right? How do you intend to know for sure if the stripping is correct if you can't display or save the answer...

I'm confused by this???Why can't you use C standard libraries? This is a C program right? How do you intend to know for sure if the stripping is correct if you can't display or save the answer...

I am guessing these are self imposed restrictions, as the OP is trying to stick with what has already been presented in K&R, and nothing more. My guess is that it is probably this exercise.

I actually found the problem....google "getline solaris".

Who needs to do a Google search for getline? Whether or not the gcc extension getline exists for Solaris machines has nothing to do with what your problem was (for if you had been trying to compile your code on a Linux box, you would have had the same problem, even though getline _does_ exist under Linux). Your problem was that you were trying to use a function that the compiler knew nothing about. The compiler knew nothing about it, because it was not declared anywhere in your code. Plain and simple.

Because I am on a Sunblade(Solaris) I cannot use getln, fgets, or basically anything other than string.h which is not supposed to be used for this program. My next way of attempting to simply solve the program will be to copy every individual character into an array using getchar and then begin copying the array into a second array that has an if statement that checks if it is a whitespace character duplicate before copying. Can anyone think of any better way to do this that does not use any external libraries or getline, getln, fgets?....basically just getchar() and putchar()

Maybe you could write your own function, and call it...oh, I don't know... getline? :idea:

Maybe you could write your own function, and call it...oh, I don't know... getline? :idea:

This guy's having problems with a simple user app an you want him to interface with the system calls directly and create a function that will mimic getline?

This guy's having problems with a simple user app an you want him to interface with the system calls directly and create a function that will mimic getline?

I didn't mean that at all. I believe that it is quite possible to do a simple 'getline' function by using getchar. It would be limited, yes, but it could be done. I don't have a copy of K&R2 at the moment, but I believe they implement (in the book) something like what I am talking about.

Edit:

There is an example of what I am talking about here.

I didn't mean that at all. I believe that it is quite possible to do a simple 'getline' function by using getchar. It would be limited, yes, but it could be done. I don't have a copy of K&R2 at the moment, but I believe they implement (in the book) something like what I am talking about.

Edit:

There is an example of what I am talking about here.

Yes but getchar requires external libraries since it requires systems calls to interact with the stream which is maintained by the operating system....Or are we talking about a non-standard C compiler

If you re-read the original post, you will see that the OP stated that there were to be no libraries used other than stdio.h. In fact, stdio.h is #included in the original code, although he missed putting it inside the code tags. I think (as a semi-educated guess) that the intent here is to complete the exercises in the book by using only what has been presented in the book thus far. Consequently, if the OP is indeed working on exercise 1-9 (I am guessing this might be the exercise) then many functions will not be available, as they have not been covered at that point of the book. Of course, the OP will be disappointed to know that malloc is a no go if only stdio.h is 'allowed.' Having said all this, I would grant that the example getline I posted a link to uses more libraries than stdio.h.

that does not use any external libraries

I must of misinterpreted this line

I must of misinterpreted this line

I think one of the issues here is that the OP has not been very clear as to the requirements/expectations of the program.

Then yes I agree that you can mimic getline with getchar/getc/fgetc

I think one of the issues here is that the OP has not been very clear as to the requirements/expectations of the program.

Because I do not want someone to go oooo here it is: "post random code". Aside from all this I am posting the updated code which now executes, yet when I compile it using a.out<trim.in>trim.out my trim.out file is all blank. Clearly I am not using putchar right to write to trim.out. I think the main issue is with my second while loop never terminating. The next update of the code will contain arrays as already stated to store each individual element, then I will read through the array containing original characters and copy it to a second array. Removing any whitespace characters that it encounters while for looping through the copy....I have not yet figured out how to allocate the size of the second array though. After I get the code correctly working, I plan to break it up into more individual functions so the code is essentially just a main calling different functions to do all the work. I feel that I might have to make it like this and use gdb to find where the error is that has my trim.out consisting of nothing more than '0'. The original code has been scrapped for this completely new approach

For Kermit:

int getline(char s[], int lim)
{
        int c, i;

        for (i = 0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
                s[i] = c;
        if (c == '\n') {
                s[i] = c;
                ++i;
                }
                s[i]='/0';
                return i;
}

New and updated code with errors of course....this program is just not my program. The following errors on compilation and I really am not too sure why. Could someone please explain?

]blade71(115)% gcc trim_3.c
trim_3.c: In function `copy_NW':
trim_3.c:43: error: invalid lvalue in assignment

blade71(119)% cat trim_3.c
#include<stdio.h>
#define MAXLINE 1000

int getline (char line[], int maxline);
int copy_NW(char to[], char from [], int NWS);

main ()
{
        int NL, WS, len;
        char line[MAXLINE];
        char line_NW[MAXLINE];
        while ((len = getline(line, MAXLINE))>0)
                {
                        WS++;
                }

        while(WS>1)
                {
                        copy_NW(line, line_NW, WS);
                }
return 0;


}

int getline(char s[], int lim)
{
        int c, i;
        for (i = 0; i<lim -1 && (c=getchar())!= EOF && c!='\n'; ++i)
                s[i]= c;
        if (c=='\n'){
                s[i] = c;
                ++i;
                        }
                s[i] = '\0';
                return i;
}

int copy_NW(char line[],  char line_NW[], int NL)
{
        int c, d;
        int i = 0;
        while(NL != 0 && c=getchar()!=EOF)
        line[i] = line_NW[i];
        if(c==' ' || c=='\n' || c=='\t')
        NL--;
        d=putchar(c);
        line_NW[i] = d;
        return d;
}

Fixed the problem however I now have a runtime error. I octal dumped the input file and used the input file with the code I wrote to produce an output file and then octal dumped it. However, my octal dump of my out file is completely wrong! Somewhere in my program there is a non terminating loop; because I had to control C.
Here is the execution:

blade71(136)% gcc trim_3.c
blade71(137)% od -x trim.in
0000000 0909 4e6f 7720 6973 2074 6865 2020 2020
0000020 2020 200a 0909 5469 6d65 2066 6f72 0909
0000040 0a09 0961 6c6c 2067 6f6f 6420 6d65 6e20
0000060 746f 2020 2020 0a09 0963 6f6d 6520 746f
0000100 2074 6865 2061 6964 206f 6620 7468 6520
0000120 7061 7274 792e 2020 2020 2020 0a0a
0000136
blade71(138)% gcc -o trim trim_3.c
blade71(144)% dir
In_Files core trim trim.c trim.in trim_2.c trim_3.c
blade71(145)% trim<trim.in>trim.out
^C
blade71(146)% od -x trim.out
0000000 ffff ffff ffff ffff ffff ffff ffff ffff
*
12260000

Here is the final source:

blade71(171)% cat trim_3.c
#include<stdio.h>
#define MAXLINE 1000

int getline (char line[], int maxline);
int copy_NW(char to[], char from [], int NWS);

main ()
{
        int NL, WS, len;
        char line[MAXLINE];
        char line_NW[MAXLINE];
        while ((len = getline(line, MAXLINE))>0)
                {
                        WS++;
                }

        while(WS>1)
                {
                        copy_NW(line, line_NW, WS);
                }
return 0;


}

int getline(char s[], int lim)
{
        int c, i;
        for (i = 0; i<lim -1 && (c=getchar())!= EOF && c!='\n'; ++i)
                s[i]= c;
        if (c=='\n'){
                s[i] = c;
                ++i;
                        }
                s[i] = '\0';
                return i;
}

int copy_NW(char line[],  char line_NW[], int NL)
{
        int c, d;
        int i = 0;
        while(NL != 0 && (c=getchar())!=EOF)
        line[i] = line_NW[i];
        if(c==' ' || c=='\n' || c=='\t')
        NL--;
        d=putchar(c);
        line_NW[i] = d;
        return d;
}

I am going to mark this thread as solved and start a new one called "Trim.c" as I just realized this thread is de-railed and has nothing to do with gcc and getline anymore. Thank you all for your help so far.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.