-1

I'm trying my luck with C lately and I came across to this question where I'm stuck.

I've a hello.c file

CODE 1

#include<stdio.h>
#include<stdlib.h>
int main(){
    printf("Hello World");
    return 0;
}

I open this file and display the content using the following C program (CODE 2)

CODE 2

#include<fcntl.h>
#include<stdio.h>    
int main() {

    FILE *fd;
    char ch;
    fd = fopen("/home/hello.c","r");

    if( fd != NULL ) {
        while((ch = getc( fd )) != EOF){
                putchar(ch);
        }
    }

    return 0;
}

However, I want the output of this code to be Hello World, i.e output of the hello.c file which is read. How can that be done?

Abhay Nayak
  • 1,069
  • 1
  • 11
  • 25
  • 1
    Unrelated to your problem, but the [`getc`](https://en.cppreference.com/w/c/io/fgetc) function returns an `int`. This is rather important for that `EOF` check. So please update your variable `ch` accordingly. – Some programmer dude Oct 01 '18 at 06:22
  • As for your problem, what is the goal with the second program? To read and *parse* the source-file `hello.c`? To compile `source.c` and run the executable created? – Some programmer dude Oct 01 '18 at 06:24
  • It is best to include a newline at the end of the output in your "Hello World" program. Either use `puts()` instead of `printf()` or add the character to the end of the string. – Jonathan Leffler Oct 01 '18 at 06:51
  • What do you think you use from the `` header? When you're using file streams (`FILE *`), you very rarely need `` too. Stylistically, it's also conventional to use `fp` for 'file pointer' and reserve `fd` for 'file descriptor'. File descriptors have type `int` and you might well need `` when you use file descriptors (and/or ``). – Jonathan Leffler Oct 01 '18 at 06:52

4 Answers4

3

In order to run a c file, first you need to compile it into machine code then execute it.

To compile it: run gcc source-file -o executable-file

To run, execute: executable-file

In order to to the same things in C, use system() function from <stdlib.h>

const char* tempFile = "./tempfile";
const char* sourceFile = "hello.c";

const char compileCommand[255];
sprintf(compileCommand, "gcc %s -o %s", sourceFile, tempFile);

system(compileCommand);

system(tempFile);

This code hasn't been tested.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Daniel Tran
  • 6,083
  • 12
  • 25
1

Your "CODE 2" would have to invoke a C-compiler to compile "CODE 1" and then run it using system() or a function provided by your operating system.

BTW: It is either int main(void) or int main(int argc, char** argv), NOT int main().

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Swordfish
  • 12,971
  • 3
  • 21
  • 43
  • Note that the C11 standard contains examples using `int main()`. Granted, they're not normative, but it isn't wholly wrong to use `int main()` — even though I strongly recommend using `int main(void)` instead. Don't overstate the case, that's all. (See [What should `main()` return in C and C++?](https://stackoverflow.com/questions/204476/) for the details.) – Jonathan Leffler Oct 01 '18 at 06:56
1

Currently, in the second program, you are reading hello.c file. So the output of CODE2 will be the contents of hello.c. i.e. #include<stdio.h>...

For what you need, in CODE1, you need to write the output of the program into a separate file (say a.txt) and then read a.txt in CODE2.

Hope this is a sufficient hint for you to solve further.

Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
0

As general solution, you may try also to have a look to a C interpreter, like Cling, and try to include it in your project.

Giovanni Cerretani
  • 1,693
  • 1
  • 16
  • 30