NO

Author Topic: int main(int argc, char *argv[], char *env[]) Related Problem  (Read 4180 times)

royski

  • Guest
Hi

Attempting to port a Linux program from book titled "C Programming in Linux) (circa 2008) to my Windows 8.1 running Pelles C latest version.  Here following is the code running in console mode.

#include <stdio.h>
#include <string.h>

                      /*  MAIN PROGRAM BODY  */

int main(int argc, char *argv[], char *env[])


   _clrscr();    // Erase the user's output screen

   printf("\n\n");

   printf("Content-type:text/html\n\n");
   printf("<html>\n");
   printf("<body bgcolor=\"%s\">\n", argv[1]);
   printf("<body>\n");
   printf("<html>\n");

   printf("\n\n");
   return 0;        // another way of ending program
   
}                      /*  END OF MAIN PROGRAM  */

Does not work in Pelles C, running on Windows 8.1.  Might be because program was written to run in Linux.  Appears to be related to the statement "int main(int argc, char *argv[], char *env[])".  The last argument does not appear to work.  I don't get the webpage to open at all.  As though the printf statements for the generation of the webpages are not working.

What am I NOT doing or doing WRONG??

Thanks in advance!!

royski
Email:  Removed. (It's not a good idea to show personal Email on a public forum, unless you like spam...)
« Last Edit: June 12, 2016, 09:13:11 PM by frankie »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: int main(int argc, char *argv[], char *env[]) Related Problem
« Reply #1 on: June 12, 2016, 09:17:46 PM »
 ;)
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
                      /*  MAIN PROGRAM BODY  */
int main(int argc, char *argv[]/*, char *env[]*/)
{
   _clrscr();    // Erase the user's output screen
   char *env = getenv("QUERY_STRING");
   printf("\n\n");

   printf("Content-type:text/html\n\n");
   printf("<html>\n");
   printf("<body bgcolor=\"%s\">\n", argv[1]);
   printf("<body>\n");
   printf("<html>\n");

   printf("\n\n");
   return 0;        // another way of ending program
}                      /*  END OF MAIN PROGRAM  */
« Last Edit: June 12, 2016, 09:42:41 PM by TimoVJL »
May the source be with you

Scripter

  • Guest
Re: int main(int argc, char *argv[], char *env[]) Related Problem
« Reply #2 on: June 12, 2016, 10:43:48 PM »
Hi

Attempting to port a Linux program from book titled "C Programming in Linux) (circa 2008) to my Windows 8.1 running Pelles C latest version.  Here following is the code running in console mode.
Code: [Select]
#include <stdio.h>
#include <string.h>

                      /*  MAIN PROGRAM BODY  */

int main(int argc, char *argv[], char *env[])
           
Does not work in Pelles C, running on Windows 8.1.  Might be because program was written to run in Linux.  Appears to be related to the statement "int main(int argc, char *argv[], char *env[])".  The last argument does not appear to work.  I don't get the webpage to open at all.  As though the printf statements for the generation of the webpages are not working.

What am I NOT doing or doing WRONG??

Thanks in advance!!

The correct main function for console programs in Windows is...
Code: [Select]
int _cdecl main(int argc, char **argv)
  {
     // lots of fun stuff

    return 0;
  }

You will also find that Linux console code is significantly different than Windows console code... so much so that porting from one to another almost always either fails or takes a ton of work.

If you are looking for a good tutorial on C programming in Windows, try this...

https://kldp.org/files/c+in+21+days.pdf

.. just save the PDF to disk for future reference.




royski

  • Guest
Re: int main(int argc, char *argv[], char *env[]) Related Problem
« Reply #3 on: June 15, 2016, 11:38:10 PM »
Thanks to the respondes, greatly appreciated and easily understood.   I correctly my program and it ran OK.

Adios Amigos.

TheRaven

  • Guest
Re: int main(int argc, char *argv[], char *env[]) Related Problem
« Reply #4 on: June 09, 2017, 06:25:09 AM »
_clrscr() from as Linux console command/function translates into a string "cls" passed into the Windows console as argument.

This confused the h3ll out of me some time ago as I cut my teeth in the black box while using Linux and FreeBSD due to the fact that I was always GUI centralized using Windows. Very easy to translate after the initial learning curve, but as stated above can/will be very time consuming especially if the original source is somewhat sloppy. Translate > optimize > enjoy.