1

I'm not sure how to word it, so the title may be unclear. Here's the line in question

strcpy (stringstore,"int1: %d\nint2: %d\nint3: %d\nint4: %d\nstring1: %s\nstring2: %s\n",int1,int2,int3,int4,string1,string2);

This is inside a for loop so the values of the ints and strings are changing. I want to store each string in stringstore. How would I be able to do this?

EDIT: Here is more code.

int id;
int year;
char title[30];
char director[30];
double price;
double length;
int i;
char stringstore[300];
FILE *outp;
outp= fopen("CurrentCatalog.txt","w");
for (i = 0;i<=*size;i++)
{
    id = entry[i].id;
    year = entry[i].year;
    strcpy(title,entry[i].title);
    strcpy(director,entry[i].director);
    price = entry[i].price;
    length = entry[i].length;
    fprintf(outp,"ID: %d\nTitle: %s\nDirector: %s\nYear: %d\nPrice: %.2f\nLength: %.2f\n",id,title,director,year,price,length);
}
fclose(outp);
  • 2
    You mean `sprintf` instead of `strcpy`, I guess? – mafso Jul 22 '14 at 16:50
  • I don't know what sprintf is. –  Jul 22 '14 at 16:51
  • 1
    Look it up. It's a standard C function and works like you try to use `strcpy` (your call won't compile). – mafso Jul 22 '14 at 16:52
  • Have a look at the [C-wiki](http://stackoverflow.com/tags/c/info) for tutorial and manuals explaining all you need to know: http://stackoverflow.com/tags/c/info – Soren Jul 22 '14 at 16:52

3 Answers3

2

Always prefer snprintf to convert any data's to string. prototypes for sprintf and snprintf

int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);

Make the following change in your code

snprintf(stringstore,sizeof(stringstore),"int1: %d\nint2: %d\nint3: %d\nint4: %d\nstring1: %s\nstring2: %s\n",int1,int2,int3,int4,string1,string2);
Sathish
  • 3,740
  • 1
  • 17
  • 28
1

Yes you can do it. The function you are looking for is sprintf()not strcpy(). Its the same syntax.

sprintf(stringstore,"int1: %d\nint2: %d\nint3: %d\nint4: %d\nstring1: %s\nstring2: %s\n",int1,int2,int3,int4,string1,string2);

EDIT: as suggested by @chris a safer way to do it is to use snprintf()

char stringstore[500];
snprintf(stringstore, 500, "int1: %d\nint2: %d\nint3: %d\nint4: %d\nstring1: %s\nstring2: %s\n",int1,int2,int3,int4,string1,string2);

EDIT2 From the new code you posted above, I suggest you merge your sprintf and your fprintf

fprintf(outp,"ID: %d\nTitle: %s\nDirector: %s\nYear: %d\nPrice: %.2f\nLength: %.2f\n",id,title,director,year,price,length);
Valentin Mercier
  • 5,256
  • 3
  • 26
  • 50
1

Regarding ...using *strcpy()* to store a string that has many variables...

The short answer is, you don't. Use sprintf() (or one of it's variants) instead. (see below)

I want to store each string in stringstore. How would I be able to do this? If you want to store each new set of data, without loosing it in subsequent the iterations of the loop, You need to make stringstore an array of strings. i.e. create it with:

char **stringstore = 0;//will provide for an array of strings when memory is allocated

Then allocate memory for it.

Edit: (per your comment)

If you do not need to keep previous information, a single string (char array) of sufficient length will work:

#define MAX_LEN 280;//arbitrary size choice as you have not specified size in your code
char *stringstore = 0;  
stringstore = malloc(MAX_LEN +1);// +1 for null terminator  

Then you can place information into stringstore like this:

sprintf(stringstore,"int1: %d\nint2: %d\nint3: %d\nint4: %d\nstring1: %s\nstring2: %s\n",int1,int2,int3,int4,string1,string2);    

Using this method places memory for stringstore onto the heap as opposed to the stack, so you will need to free it when done using it.

free(stringstore);  

Note:
There may be other questions you should be asking about your code, such as
after opening the text file, how do I read the data into my variables? .
(You open the text file, then enter the loop, but have
never read data from the file to populate the variables used in the loop.)

ryyker
  • 22,849
  • 3
  • 43
  • 87
  • I don't want to store the previous. This is only a temporary holder of the string. –  Jul 22 '14 at 17:06