?
Solved

Problem with arrays and files

Posted on 1998-09-11
5
Medium Priority
?
175 Views
Last Modified: 2010-04-15
When I executes the program given below,and check the contents of the file to which the contents of array are supposed to be written,there garbage is stored,instead of the array contents.Can anybody tell me what is the problem?
 
#include <stdio.h>

int main(void)
{
   FILE *in;
   char msg[] = {1,2,3,4,5,6,7};
   int i = 0;
   in=fopen("file","w");
   while (msg[i])
      putc(msg[i++],in);
   fclose(in);
   return 0;
}
0
Comment
Question by:SHAHZAD
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
sganta earned 60 total points
ID: 1252646
Hi !

It should be like this
#include <stdio.h>

    int main(void)
    {
       FILE *in;
       char msg[] = {'1','2','3','4','5','6','7'};
       int i = 0;
       in=fopen("file","w");
       while (msg[i])
          putc(msg[i++],in);
       fclose(in);
       return 0;
    }
0
 
LVL 84

Expert Comment

by:ozo
ID: 1252647
  char msg[] = {'1','2','3','4','5','6','7','\0'};
or
   char msg[] = "1234567";
0
 
LVL 4

Expert Comment

by:sganta
ID: 1252648
Yes, ozo is right !
Thank you ozo for correcting my mistake.
0
 

Author Comment

by:SHAHZAD
ID: 1252649
what if the array comes from other part of the program and is very large(100x100).How would i handle that.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1252650
That may depend what you're getting from the other part of the program, and what you want to do with it, but perhaps you'd want

  putc('0'+msg[i++],in);

or

  fprintf(in,"%d,",msg[i++])'
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

762 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