Jump to content

A C beginner here [Help with fwrite]

- - - - -

  • Please log in to reply
18 replies to this topic

#1
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Hello,

I'm trying to build a program that takes from user some inputs (Name, Surname, telephone...) using scanf() and write them in a txt file. My problem is that still I'm learner so until now I have something like...

.

.

.

printf("Enter name: \t");

	scanf("%s", &name);

	fprintf(fp,"Name:\t");

	fwrite(name,1, sizeof(name), fp);

	printf("Enter surname: \t");

	scanf("%s", &surname);

	fprintf(fp,"\nSurname:\t");

	fwrite(surname,1, sizeof(surname), fp);

	fclose(fp);

.

.

.

The result prints in file just "Name: xxxxx"

What I'm doing wrong with fwrite() ?

Thank you,
toto_7

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
First of all, how do you define the 'name' variable? 'char name [512];' ? If so, you do NOT need the '&' when you reference it in the scanf () function call:

toto_7 said:

scanf ("%s", &name)

Then there's the fprintf () call; why not just use fprintf () to write the name to the file? Like this:
fprintf (fp, "Name: \t%s", name); 

And even if you did use fwrite () for writing the name, you don't need to use 'sizeof' for finding the length of 'name' ; so instead of

toto_7 said:

sizeof (name)
it should be
strlen (name)
(You'll also have to include 'string.h' , in this case.)

#3
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Thank you RhetoricalRuvim working perfectly. So the problem was "sizeof (name)" ?? I'm using fprintf() too, but teacher has as advice some functions so advice mean "Use them or no marks!!!" heh :) Thank you again

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#4
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
sizeof (name) should give the size of the variable, not in respect to the content; I am not absolutely sure, however, if it would give the size of the point (4) or the size of the array (512 or whatever the size). In any case, getting the length of a string, with regards to what it contains, is done using a function like strlen ().

Also, functions like printf (), scanf (), sscanf (), etc., can be found in some other languages too, such as PHP, so if you learn how to use the functions, you might be able to use them in other languages too.

#5
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Thank you :) In addition what happening if you want to write an int variable in the file?

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#6
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
It depends on how you want to write it. If it's a binary file, you'd use something like 'fwrite (&the_int_to_write, 1, sizeof (int), file_handle)' . If it's a text file, on the other hand, you'd do something like 'fprintf (file_handle, "%d", the_int_to_write)' .

#7
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Found it thank you. Just another question, when user typing year(int) if the input is not 4-digit (xxxx) then program stops writing the data in the file. Why this happening? Program executed fine, but at the end when open the file is written until "Year: 99 (ex.)" ignore the rest

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#8
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
May need to see how you did this to figure it out.

---------- Post added at 07:22 PM ---------- Previous post was at 07:09 PM ----------

main()
 
 {
    char name[512];
    char * names;
    char name_c;
    
    printf("size of name: %d\n",sizeof(name));
    printf("size of name: %d\n",sizeof(names));
    printf("size of name_c: %d\n",sizeof(name_c));
 }

would produce
size of name: 512
size of names: 4
size of name_c: 1

Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#9
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US

toto_7 said:

Found it thank you. Just another question, when user typing year(int) if the input is not 4-digit (xxxx) then program stops writing the data in the file. Why this happening? Program executed fine, but at the end when open the file is written until "Year: 99 (ex.)" ignore the rest

What are you doing? This?:
int year; 

scanf ("Enter the year: %s", &year); 

Because if so, you're not supposed to do that; if you want to get text from the user, you use a string:
char the_string[512]; 

scanf ("Enter the year: %s", the_string);     // Since the_string is an array, we don't need the '&' to say we use the address of the_string, and not the content. 

If you want to get a number from the user, you would do something more like this:
int year; 

scanf ("Enter the year: %d", &year); 

So that then 'year' would be the variable that has the actual number that the user entered, and not the 4-byte array of what the user entered.

#10
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Hello RhetoricalRuvim ,

Thank you fread as well. I made some changes in my code (fprintf instead of fwrite) so everything is ok. But, I would like to ask because I'm not so familiar with pointers. I have a char [] xxx and a function that takes as parameter myFunction(char *xxx), how I will take just an element of that array to make a comparison for example in that function?

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#11
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
Why take a char * as an argument when you can just take a char. Anyways try this:
void function(char * c)
 {
    if ('m' == *c) printf("found char\n");
 }
main()
 
 {
    char name[512];
    char * ptr;
    scanf("%s",name);
    ptr = &name[0];
    printf("I have this char: %c\n",*ptr);
    function(ptr);
 }

Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#12
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Thank you for quick response, what will happen if pass as a parameter from main the array, name? function(name); ?
Basically, I trying to check in function if elements in the array is lowercase or upercase

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users