Related Posts Plugin for WordPress, Blogger...

Thursday, October 25, 2012

How to write to a file in C ~ PCbots

Hello, Me sorry really came after long time :)

Last night i was working on code just like a space eater malware for Linux. It is really easy to create and write some in file using C++ but in C it is little issue because of lack of  fstream.h header file. i find solution so though i should share with you peoples :)

This will work for both Linux and Windows.


// Author: Muhammad Usman // Name: Create_file_Read_Write.c// Date: 07/4/2012 // Description: Opens file by append example

   #include <stdio.h>   #include <sys/types.h>   #include <sys/dir.h>            int main()   {    /* Buit in C function to create Directory */    int c = mkdir("FolderName",0777); /* 2 Arguments, 1st one for folder name and 2nd one is for permission */    if(c==0) /* Check to confirm that directory is created or n0t */    {          printf("New Directory is Created \n");    }     else     {            printf("Error in Creation of Directory \n");     }
    FILE *file;  /* Create a File Pointer */        file = fopen("FolderName/file.txt","a+");  /* apend file (add text to a file or create a file if it does not exist.*/     fprintf(file,"%s","This is just an example and Mad person here :)"); /*writes*/     fclose(file); /*done!*/ 
    getchar(); /* pause and wait for key */ 
    return 0;}


// To run
// Just open Linux terminal
vim test.c [enter]
copy paste the code

// To save press Esc and then Write
:wq
// This will save(w) and Quit(q)

// To Compile
g++ -o test test.c (enter)

// to run file
./test

BingO!

Use Full Link: http://www.cplusplus.com/reference/clibrary/cstdio/fopen/

1 comments:

Anonymous said...

nice, but you need a better way to organize your code brother :P

Post a Comment