ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Please put your code between [code] and [/code]. Thanks.
Although smeezekitty's solution will work, your code is not C++ code and therefore I don't see the need to use g++.
Change the extension of your source to '.c' so gcc does not think that it's c++ code. Check man gcc for file extensions and their effects.
I've copied your code into a file xx.c and fixed two warning regarding the return value of main. Next I copied it to xx.cpp so both files are the same.
Code:
wim@btd-techweb01:~/progs$ ls -l xx*
-rw-r--r-- 1 wim develop 891 2009-09-21 09:38 xx.c
-rw-r--r-- 1 wim develop 891 2009-09-21 09:40 xx.cpp
wim@btd-techweb01:~/progs$ diff xx.c xx.cpp
wim@btd-techweb01:~/progs$ gcc -Wall xx.c -o xx1
wim@btd-techweb01:~/progs$ gcc -Wall xx.cpp -o xx2
xx.cpp: In function `int main()':
xx.cpp:53: warning: comparison between signed and unsigned integer expressions
xx.cpp:98: warning: comparison between signed and unsigned integer expressions
/tmp/ccewiL69.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
wim@btd-techweb01:~/progs$ g++ -Wall xx.cpp -o xx2
xx.cpp: In function `int main()':
xx.cpp:53: warning: comparison between signed and unsigned integer expressions
xx.cpp:98: warning: comparison between signed and unsigned integer expressions
wim@btd-techweb01:~/progs$ ls -l xx*
-rw-r--r-- 1 wim develop 891 2009-09-21 09:38 xx.c
-rw-r--r-- 1 wim develop 891 2009-09-21 09:40 xx.cpp
-rwxr-xr-x 1 wim develop 12118 2009-09-21 09:53 xx1*
-rwxr-xr-x 1 wim develop 12529 2009-09-21 09:53 xx2*
wim@btd-techweb01:~/progs$
As you can see, I did not have a problem compiling your code using gcc (xx.c). When I compiled xx.cpp with gcc it complained with the error that you have. And using g++ indeed solved that (I was too lazy to fix the 2 warnings).
Please note the extra size required when using C++.
Last edited by Wim Sturkenboom; 09-21-2009 at 03:06 AM.
Reason: added ref to man gcc
Debug Either learn to use gdb or put some printf's or at strategic places (my approach).
Your problem is probably the strlen(com) as I don't see that com is a NULL terminated string.
Other mistakes that I quickly see
You don't test the value of fi and fo after calls to fopen; so if chk.txt does not exist you will happily continue.
You don't close fi and fo before re-using them.
And I think that there are a couple of other issues regarding the use of getc on a file that you open for writing. Read the man pages of all the functions that you use.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.