Link to home
Start Free TrialLog in
Avatar of yabelson
yabelson

asked on

writing to files with no actual name

as a continuall to my former qustion(problem with writing to files) i have written something with your help.only problem-i am not sure that it will work,and if it will-how.the code compiles.here it is:
k=0;
char filename[10];
while(k<flag)
{
        sprintf(filename,"file%02u.dat",k);
        for(j=0;j<(sizeof(file[k]));j++)
     fprintf(*(filename+k),"%d ",file[k][j]);

     fclose(filename);

     k++;
}
can you please help me?
Avatar of yabelson
yabelson

ASKER

i have another problem:how do i print these files afterwards?
You've lost me, what does the code hope to do?
>>only problem-i am not sure that it will work,and if it will-how.the code compiles.
You'll never know until you'll run it. That's what you generaly do after compilation...

--EC--
Once again, what are you trying to do? sizeof(file[k]) should probably be strlen(file[k])...
Avatar of Paul Maker
before fprintf should you not open the file and get a handle or FILE pointer ?
ASKER CERTIFIED SOLUTION
Avatar of Paul Maker
Paul Maker
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
makerp

Please be careful what you are doing. As yabelson stated in qid=20136758, this is homework and we are not supposed to do his homework. If students have questions, we can help, but giving them the complete code for their assignement is against EE policy.
I think he has to understand the file descriptors first before he can do things like this.

yabelson

You have open questions here and received answers. You can't just open a new question and abondon an old one for that, that is not fair for the experts that tried to help you. You have always the option to accept a comment as an answer.

======
Werner
i had no idea that this was homework, furthermore i dont have the time to search through the whole C section looking for the old question he mentioned.

sorry anyway
response:
a.this is a very small section(about 3%) of my code,and i have written a specific qustion.i am not famliiar with this command(i.e.(filename,"FILE%u.dat"))and i wanted to understand how it works.
b.these homework are long overdue,i just want to do it to know exactly how it would work.
c.i only opened the next qustion after i didnt get any reply for three days on the other one.i made myself more clear this time,tihnking that the first one wasnt clear enough.

as i said:i only wanted remarks on what i have done,and suggestions on how to write back from it.
if you can help me with this command,i will be gratefull(since i dont know its syntax and cant find it on the help menu,and believe me,i tried...);
thanks,yair.
yabelson

c: If you have to clarify a question, you don't have to open a new question. If you think you have to, please close the other question in an appropriate way (You can always delete a question that didn't get an answer). You have three open question for the same problem now!

To your problem: To be able to write to a file, you have to get a handle first (that's what fopen() gives you, it connects a filename with a handle). All the read/write request don't use the filename, they just require the handle from fopen(). When you are done you issue an fclose() command to close the file.

If you are using VC++ you can always look up the help file by searching for "fopen". A little sample program will always help to understand these commands.

Good luck

======
Werner
i know about fopen and fclose,but i didnt understand what names the files that i created got.if i want to print them at request, i have to know which one to open.if i dont know thier names(this is the problem i had at the beginning) i cant open them,and cant print them.so,now my problem is:what names do i get when i use the FILE%u.dat function in the command line?
i think this will solve all my problems,and i will fianally have this question fully understood,and working:-)
what does the function perror() do?
perror() prints a formatted error message based on the value in errno (a global error code set by many functions.)

You call it with a string of your choice as follows...

perror("Error in function xyz: Error - ");

perror will then print an error message to stderr along the following lines

"Error in function xyz: Error - The text relating to the current value of errno." followed by a newline

yabelson

look up the sprintf function in your compiler documentation and you will see how the "%u" gets replaced by the parameter "k"

======
Werner
this is exactly the problem.i am working with boreland and when i am looking for sprintf i get all the functions related with printf.even when i find specific mterial about sprintf through that,i cant find %u speifications.
so i am asking for the command that allows me to print after i used a %u parametere at sprintf,and an explanation on how it works.
sprintf work the same as printf but with ine small difference. printf prints to the stdout and sprintf prints out into a variable (the first parameter). thats why sprintf documenttation is not good as it wouls just be printf documenttation. the bulk of all this is the formatting/precision etc
I wasn't aware that the Borland documentation is that bad:-). Here is a part of the man page for the sprintf() function with the formatting information:

         
o The following characters indicate the type of conversion to be applied:
    %
      Performs no conversion. Prints (%).
    d or i
      Accepts a Value parameter specifying an integer and converts it to signed
      decimal notation. The precision specifies the minimum number of digits to
      appear. If the value being converted can be represented in fewer digits,
      it is expanded with leading 0's. The default precision is 1. The result of
      converting a value of 0 with a precision of 0 is a null string. Specifying
      a field width with a 0 as a leading character causes the field-width value
      to be padded with leading 0's.
    u
      Accepts a Value parameter specifying an unsigned integer and converts it
      to unsigned decimal notation. The precision specifies the minimum number
      of digits to appear. If the value being converted can be represented in
      fewer digits, it is expanded with leading 0's. The default precision is 1.
      The result of converting a value of 0 with a precision of 0 is a null
      string. Specifying a field width with a 0 as a leading character causes
      the field-width value to be padded with leading 0's.
    o
      Accepts a Value parameter specifying an unsigned integer and converts it
      to unsigned octal notation. The precision specifies the minimum number of
      digits to appear. If the value being converted can be represented in fewer
      digits, it is expanded with leading 0's. The default precision is 1. The
      result of converting a value of 0 with a precision of 0 is a null string.
      Specifying a field-width with a 0 as a leading character causes the field
      width value to be padded with leading 0's. An octal value for field width
      is not implied.
    x or X
      Accepts a Value parameter specifying an unsigned integer and converts it
      to unsigned hexadecimal notation. The letters abcdef are used for the x
      conversion and the letters ABCDEF are used for the X conversion. The
      precision specifies the minimum number of digits to appear. If the value
      being converted can be represented in fewer digits, it is expanded with
      leading 0's. The default precision is 1. The result of converting a value
      of 0 with a precision of 0 is a null string. Specifying a field width with
      a 0 as a leading character causes the field-width value to be padded with
      leading 0's.
    f
      Accepts a Value parameter specifying a double and converts it to decimal
      notation in the format [-]ddd.ddd. The number of digits after the decimal
      point is equal to the precision specification. If no precision is
      specified, six digits are output. If the precision is 0, no decimal point
      appears.
    e or E
      Accepts a Value parameter specifying a double and converts it to the
      exponential form [-]d.ddde+/-dd. One digit exists before the decimal
      point, and the number of digits after the decimal point is equal to the
      precision specification. The precision specification can be in the range
      of 0-17 digits. If no precision is specified, six digits are output. If
      the precision is 0, no decimal point appears. The E conversion character
      produces a number with E instead of e before the exponent. The exponent
      always contains at least two digits.
    g or G
      Accepts a Value parameter specifying a double and converts it in the style
      of the e, E, or f conversion characters, with the precision specifying the
      number of significant digits. Trailing 0's are removed from the result. A
      decimal point appears only if it is followed by a digit. The style used
      depends on the value converted. Style e (E, if G is the flag used) results
      only if the exponent resulting from the conversion is less than -4, or if
      it is greater or equal to the precision. If an explicit precision is 0, it
      is taken as 1.
    c
      Accepts and prints a Value parameter specifying an integer converted to an
      unsigned char data type.
    C
      Accepts and prints a Value parameter specifying a wchar_t wide character
      code. The wchar_t wide character code specified by the Value parameter is
      converted to an array of bytes representing a character and that character
      is written; the Value parameter is written without conversion when using
      the wsprintf subroutine.
    s
      Accepts a Value parameter as a string (character pointer), and characters
      from the string are printed until a null character (\0) is encountered or
      the number of bytes indicated by the precision is reached. If no precision
      is specified, all bytes up to the first null character are printed. If the
      string pointer specified by the Value parameter has a null value, the
      results are unreliable.
    S
      Accepts a corresponding Value parameter as a pointer to a wchar_t string.
      Characters from the string are printed (without conversion) until a null
      character (\0) is encountered or the number of wide characters indicated
      by the precision is reached. If no precision is specified, all characters
      up to the first null character are printed. If the string pointer
      specified by the Value parameter has a value of null, the results are
      unreliable.
    p
      Accepts a pointer to void. The value of the pointer is converted to a
      sequence of printable characters, the same as an unsigned hexadecimal (x).
    n
      Accepts a pointer to an integer into which is written the number of
      characters (wide-character codes in the case of the wsprintf subroutine)
      written to the output stream by this call. No argument is converted.
i got by the sprintf,nut now i am stuck with the scanf and its parameters.apperantly,i am tryiing to transfer a FILE to FILE*,and in the same manner,i cant close the file:*fd is a pointer,and i cant close a pointer (...).
here is the fix i did,can you tell me what is wrong wtih this code?(bear in mind the errors i get...)

puts("which file do you want to see?");
for(i=0;i<size;i++)
{
     printf("1. file no. %d\n");
}
scanf("%d",&temp);

k=0;
i=0;
while(k<flag)
{
     size=0;
     while(i<lengthofb)
     {
          if((*(fb+i))==(*(fb+i+1)))
               size++;
          else
               break;
     }
for(j=0;j<size;j++)
     sprintf(filename,"file%04u.dat",k);
FILE *fd=fopen(filename,"wb");
if(temp==k)
{
for(j=0;j<size;j++)
{
fscanf(fd[j],"%d",&temp1);
printf("the %dst member of the mahlaka is %d\n",j,temp1);
     }
}
         
fclose(fd);
k++;
}

it didnt answer my q fully,but it did get me closer to the finish line