[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Unable to open a file.

Posted on 2004-11-08
2
Medium Priority
?
340 Views
Last Modified: 2010-04-15
extern"C" {
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
}

struct account
{
 char *name;
 float cur_exp,prev_debit, prev_credit;
 struct account *next;
 };
typedef struct account acc;
display(acc * );
acc add(acc **);
int main()
{
int i;
clrscr();
acc *list_acc;
list_acc = NULL;
clrscr();
//Print the various options available to the user
printf("1. View the records\n");
printf("2. Calcuate current per head expenditure\n");
printf("3. View total expenditure till date\n");
printf("4. Who spends the most\n");
printf("5. Graphical representation\n");
printf("6. Add/Delete a member\n");
scanf("%d", &i);
switch(i)
 {
   case 1: printf ("Viewing records");
         add(&list_acc);
         display(list_acc);
         break;

   case 2:
   case 3:
   case 4:
   case 5:
   case 6:
   default:
   printf("Input data unknown\n");
  };
getch();
}
display(acc *list_ptr)
 {
    while(list_ptr != NULL)

    {
      printf("\n %s" ,list_ptr->name);
      list_ptr  = list_ptr->next;
     }
  }
acc add(acc **list_ptr)
 {
   FILE *fp;
   acc *temp, *new_blk;
   temp = *list_ptr;
   int i,number;
   printf(" Enter the number of people in your house");
   scanf("%d", &number);
   fp = fopen("c:\tcplus\tc\data.txt","w");
   if(fp==NULL)
   printf("Error opening file\n");   //Unable to open file is a positive case in my program.
   for(i=1;i<=number;i++)
   {
        new_blk = (acc *)malloc(sizeof(acc));
        printf("\nEnter the name of person %d", i);
        scanf("%s",new_blk->name);
        new_blk->next = NULL;
        fprintf(fp,"%s\n",new_blk->name);
        if(*list_ptr == NULL)
         {
         temp = new_blk;
         *list_ptr = temp;
        }
        else
         {
         temp->next = new_blk;
         temp = temp->next;
       }

   }
   }

Thnks.
0
Comment
Question by:ksanand_be
2 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 2000 total points
ID: 12527694
A backslash is an escape character in C. It is used to denote control characters such as newline (\n) and carriage return (\r). Thus, in order to put a backslash in a string (i.e. in order to avoid the compiler treating it and the character following it as a control code) you have to put down two backslashes. That is, to get a backslash in a string you must write "\\".

So, you're filename should be written as:

fp = fopen("c:\\tcplus\\tc\\data.txt","w");

Otherwise the compiler will treat the \t's as tabs.

0
 

Author Comment

by:ksanand_be
ID: 12527744
Brilliant! Thanks a lot! The code works to the perfection.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses
Course of the Month17 days, 14 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question