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;
} 
Judge Maygarden
  • 26,961
  • 9
  • 82
  • 99
Satish
  • 33
  • 1
  • 1
  • 3
  • Ouch, please reformat your post to take advantage of SO's syntax highlighting (not to mention multiple lines!) – Asher Dunn Dec 22 '09 at 05:08
  • is this 'FILE *fopen(),' a typo ? – Andrew Keith Dec 22 '09 at 05:10
  • 2
    So you don't get in trouble with backslashes, always write pathnames with forward slashes: `fopen("D:/Satish_SharedSubstance/V13.4-CT_Testing/LONGRUN_Testing/writetest.txt","w");` This is guaranteed to work on all platforms, even OpenVMS, MSDOS, and Windows. – wallyk Dec 22 '09 at 05:25

3 Answers3

4
 fp = fopen("D:\Satish_SharedSubstance\V13.4-CT_Testing\LONGRUN_Testing\writetest.txt","w");

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

aJ.
  • 34,624
  • 22
  • 86
  • 128
4

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
  • 26,961
  • 9
  • 82
  • 99
0

You can do the mentioned below:-

FILE *fp = fopen("D:\\Satish_SharedSubstance\\V13.4-CT_Testing\\LONGRUN_Testing\\writetest.txt","w");  
Abhineet
  • 6,459
  • 10
  • 35
  • 53