tags:

views:

126

answers:

3

I wrote the following C program to write data into a file.The program got compiled properly but nothing is getting written in the file.Please suggest modifications if required.

#include <stdio.h>
#include <errno.h>

int main()
{
    int i;
    FILE *fopen(),*fp;
    fp = fopen("D:\Satish_SharedSubstance\V13.4-CT_Testing\LONGRUN_Testing\writetest.txt","w");
    /*Create a file and add text*/
    if(fp!=NULL)
    {
     fprintf(fp,"GRP \n");
     fprintf(fp,"groupname group_1 \n");
     fprintf(fp,"groupcomment group_1\n");
     fprintf(fp,"jobnet 255 \n");
     fprintf(fp,";\n");
     for (i=1;i<=255;i++)
     {
      fprintf(fp,"GNT \n");
      fprintf(fp,"jobnetname jobnet_t%d\n",i);
      fprintf(fp,"jobnetnumber %d\n",i);
      fprintf(fp,";");
     }
     /*writes data to the file*/
     fclose(fp); /*done!*/ 
    }
    else
    {
     printf("Error opening file\n");
    }
    return 0;
}
+3  A: 
 fp = fopen("D:\Satish_SharedSubstance\V13.4-CT_Testing\LONGRUN_Testing\writetest.txt","w");

Try replacing "\" with "\\" in Path.

aJ
and check the root of your D: drive for a file with a REALLY LONG NAME :)
John Knoeller
Thanks John:) you were absolutely right !
Satish
+3  A: 

Two things:

  1. Get rid of the *fopen() in the variable declaration.
  2. Backslashes must be escaped in C strings. Replace each '\' with a '\\'.
Judge Maygarden
A: 

You can do in this way Suppose there is a folder called "XYZ" at "E:" drive & the file name is "abc.log" and the function which u call is say "WSL".

VOID __cdecl WSL(char *message);//function declaration

//Function definition:

VOID __cdecl WSL(char *message) 
{ 
   static int lineNo = 1;  
   FILE *fp = fopen("E:\\XYZ\\abc.log","a+");  
   if(fp!= NULL)  
   {   
        fprintf(fp,"%d : %s\n",lineNo++,message);   
        fclose(fp);
   } 
}

and at main function u r coding the following:

int main()
{
  FILE *fp = fopen("E:\\XYZ\\abc.log","w");
  WSL("Application opened");
  ........
  ........
  ........
  ........
  ........
  WSL("Application closed");
}

This will work. And dont forget to include header files.

Abhi
It is just an example....
Abhi