13 Replies - 4763 Views - Last Post: 22 December 2009 - 06:37 AM Rate Topic: -----

#1 awfa3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-July 09

Writing an integer to file in C

Post icon  Posted 21 December 2009 - 05:10 PM

I'm getting an error I think is a segmentation fault in the code.
void saveData(FILE *profile, int *data) {
	int i;
	for (i = 0; i < 11; ++i) {
		fputs(*(data + i) + '0',profile);
		fputs("\n", profile);
	}
}


Also this warning: passing arg 1 of `fputs' makes pointer from integer without a cast : on the line that says fputs(*(data + i) + '0',profile);

Arguments passed to this function are the file pointer of profile set to "w", and the pointer to the beginning of an array of integers going up to 10 (11 Elements total starting from 0)

Running the program results in a crash when the code reaches this point.

Hopefully, this will be clearer than my other posts :P

Is This A Good Question/Topic? 0
  • +

Replies To: Writing an integer to file in C

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Writing an integer to file in C

Posted 21 December 2009 - 05:19 PM

Tell us what you think this line does
fputs(*(data + i) + '0',profile);


Was This Post Helpful? 0
  • +
  • -

#3 awfa3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-July 09

Re: Writing an integer to file in C

Posted 21 December 2009 - 05:22 PM

View Postjanotte, on 21 Dec, 2009 - 04:19 PM, said:

Tell us what you think this line does
fputs(*(data + i) + '0',profile);



I think it takes data[i], adds it with '0' so its in ascii, and shoves it in profile. Also changing it to fputs(data[i] + '0',profile); makes no difference. Although it looks better :P
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Writing an integer to file in C

Posted 21 December 2009 - 05:35 PM

And "data" is a pointer to a character array?

I'm confused by the title of your posting versus its contents.

EDIT = Correct my language

This post has been edited by janotte: 21 December 2009 - 05:36 PM

Was This Post Helpful? 0
  • +
  • -

#5 awfa3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-July 09

Re: Writing an integer to file in C

Posted 21 December 2009 - 05:39 PM

Data is a pointer to the beginning of an integer array.
Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Writing an integer to file in C

Posted 21 December 2009 - 06:10 PM

Good.
I'm still not sure what you are trying to achieve.

So if data[0] holds 82 and data[1] holds 49 what would you expect to have written to the file?
Was This Post Helpful? 0
  • +
  • -

#7 awfa3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-July 09

Re: Writing an integer to file in C

Posted 21 December 2009 - 06:15 PM

Well the file already contains this:
0
0
0
0
0
0
0
0
0
0
0

So I'm just overwriting it. If data holds what you says the result would be after writing:
82
49
0
0
0
0
0
0
0
0
0

And that's what I expect to achieve
Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Writing an integer to file in C

Posted 21 December 2009 - 06:28 PM

And the reason you are using fputs()
http://www.cplusplus...y/cstdio/fputs/
instead of fputc()?
http://www.cplusplus...y/cstdio/fputc/

Sorry your design is passing me right by.
Could well say more about me than you.
Was This Post Helpful? 0
  • +
  • -

#9 awfa3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-July 09

Re: Writing an integer to file in C

Posted 21 December 2009 - 06:33 PM

Well, I tried it, it compiled normally, but there are is an error, how do I manage multi-digit integers using fputc? (10, 100, 1000)
Was This Post Helpful? 0
  • +
  • -

#10 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Writing an integer to file in C

Posted 21 December 2009 - 06:41 PM

Sorry I am still not getting what you are up to.

Are you able to supply me a little driver program to compile with your function so I can see it working and maybe grasp your design a bit better.

Alternatively could someone who knows a lot more C than me and can see what the intent is here come in here now, please.
Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,473
  • Joined: 23-August 08

Re: Writing an integer to file in C

Posted 21 December 2009 - 06:58 PM

#include <stdio.h>

void saveData(FILE *profile, int *data, const int len)
{
    int i = 0;
    for (; i < len; ++i)
        fprintf(profile, "%d\n", data[i]);
}

int main()
{
    int myInts[] = { 1, 167, 257, 984, 1032, 1894, 24002 };

    FILE *fp = fopen("mydata.txt", "w");
    if (fp)
    {
        saveData(fp, myInts, sizeof(myInts)/sizeof(myInts[0]));
        fclose (fp);
    }
    return 0;
}



???

Output:
1
167
257
984
1032
1894
24002


Was This Post Helpful? 0
  • +
  • -

#12 awfa3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-July 09

Re: Writing an integer to file in C

Posted 21 December 2009 - 07:05 PM

Thanks a lot :D It works :)
Was This Post Helpful? 0
  • +
  • -

#13 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Writing an integer to file in C

Posted 21 December 2009 - 07:09 PM

View Postawfa3, on 21 Dec, 2009 - 06:05 PM, said:

Thanks a lot :D It works :)


So you weren't setting yourself the challenge of using a "put" variant?

I am so stupid making assumptions (and close to impossible ones at that).
Was This Post Helpful? 0
  • +
  • -

#14 vvv2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-December 09

Re: Writing an integer to file in C

Posted 22 December 2009 - 06:37 AM

View Postjanotte, on 21 Dec, 2009 - 06:09 PM, said:

So you weren't setting yourself the challenge of using a "put" variant?


put() write data in the binary format,
this is more fast, but can't to be show in ascii...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1