#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *in()
{
char *a[100];
scanf("%s",&a);
printf("\na : %d",&a);
return(a);
}
int main()
{
char *b;
int i,j;
FILE *fp;
fp=fopen("asif.txt","w");
b=in();
printf("\nb : %d",b);
for(j=0;j<strlen(b);j++)
{
putc(b[j],fp);
}
fclose(fp);
return 0;
}

- 3,513
- 3
- 34
- 55

- 1
- 2
-
Please read [the editing help](http://stackoverflow.com/editing-help) to learn how to format your post. But before that you should probably read [the help pages](http://stackoverflow.com/help) and [how to ask good questions](http://stackoverflow.com/help/how-to-ask). You probably need to [find a good beginners book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) too, and read up on pointers and arrays, and scoping. – Some programmer dude Apr 13 '17 at 05:46
-
Right beside `putc()`, there's also a [`puts()`](https://linux.die.net/man/3/puts). – dhke Apr 13 '17 at 05:48
-
Why used & in printf()?? – msc Apr 13 '17 at 05:48
-
2`return(a);` is always wrong. Because it is returning a pointer to a local function variable which is not valid after the function exits. Also, `char *a[100];` should be `char a[100];`. – kaylum Apr 13 '17 at 05:49
-
i was printing address &a using – Asif Shaikh Apr 13 '17 at 05:50
-
1store `strlen(b)` in a variable outside the loop. You're calling it again and again each for loop – phuclv Apr 13 '17 at 06:02
2 Answers
You seem to have a number of fundamental misconceptions about C syntax that are giving you problems, e.g.
char *in()
{
char *a[100];
scanf("%s",&a);
printf("\na : %d",&a);
return(a);
}
char *a[100];
declares an'array or pointers to char'
(100
of them)- It is declared local inside
in
and thus will be destroyed when functionin
returns (the function stack-frame is destroyed/released at that point). - You do not need an
'array or pointers to char'
, you need either acharacter-array
of sufficient size to hold the longest word read as input, or you need to allocate a block of memory of that same size (providing space for the nul-terminating character at the end) printf("\na : %d",&a)
attempts to print the address of the array of pointers using a decimal format specifier. If you are attempting to print a pointer address, use%p
.- However, if you want to print the current word read as input, you need to use
%s
to print the string. - In either case, you would NOT use the address of operator before
a
. - You declare
in
as typechar *
, but then attempt to return typechar *[100]
- your compiler should be spewing warnings and errors (read them, you can learn more C from reading and eliminating all warnings and errors, than just about anywhere else)
It would make in
more useful to pass FILE *fp
as a parameter and use fscanf
within the function to allow reading from any file. Simply pass stdin
to read from standard input.
You also need to pass a character array of sufficient size to in
. Providing storage for b
in main
and passing it as a parameter to in
is what it looks like you are intending to do. (you can also dynamically allocate memory in in
with malloc
, calloc
, or realloc
, but then you have the responsibility to free
any memory you allocate. Better to just create storage for b
in main
and pass it to in
.
Putting that together, it looks like you intended something similar to the following:
#include <stdio.h>
#define MAX 100 /* longest word in the unabridged dictionary is 27-chars */
char *in (FILE *fp, char *a)
{
if (fscanf (fp, "%99s", a) != EOF) { /* always validate ALL input */
printf ("a : %s\n", a);
return(a);
}
else
return NULL; /* return NULL on EOF to indicate done reading */
}
int main (void)
{
char b[MAX] = ""; /* declare b with 100 chars and initialize */
FILE *fp;
if (!(fp = fopen ("asif.txt","w"))) { /* validate ALL file openingings */
fprintf (stderr, "error: file open failed 'asif.txt'.\n");
return 1;
}
while (in (stdin, b)) /* for each word in input */
fprintf (fp, "%s", b); /* write it to output file */
fclose(fp); /* close file */
return 0;
}
Example Input
$ cat data.txt
Abbie 3.4 Oakley 3.5 Sylvia 3.6 Uwe 3.7 Ken 3.8 Aaron 3.9 Fabien 4
Example Use/Output
$ ./bin/fprintftofile <dat/data.txt
a : Abbie
a : 3.4
a : Oakley
a : 3.5
a : Sylvia
a : 3.6
a : Uwe
a : 3.7
a : Ken
a : 3.8
a : Aaron
a : 3.9
a : Fabien
a : 4
File Output
$ cat asif.txt
Abbie3.4Oakley3.5Sylvia3.6Uwe3.7Ken3.8Aaron3.9Fabien4
(note: you haven't put any spaces between the words written to 'asif.txt'
or included any of the '\n'
s)
Look it over and let me know if you have any questions, or if your intent was different than I took it to be.
Note: Always compile with warnings enabled (e.g. with -Wall -Wextra
in your compile string) and do not accept code until it compiles without warning or error.

- 81,885
- 6
- 58
- 85
I can't say I've entirely understood what you are looking for. What I have understood so far is that you are asking if you can put multiple char at once in a file. My answer to that would be that fwrite() or puts() are probably what you are looking for.

- 320
- 2
- 6
-
[fprintf()](http://www.tutorialspoint.com/c_standard_library/c_function_fprintf.htm)... – Bob__ Apr 13 '17 at 06:27
-
Oh... Well... That's because... OK, I had completely forgotten fprintf existence. My bad. I believe the author don't need the string formatting though. – Kuu Aku Apr 13 '17 at 06:34