Solved

writing to files with no actual name

Posted on 2001-06-17
19
239 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:yabelson
  • 7
  • 4
  • 4
  • +3
19 Comments
 
LVL 1

Author Comment

by:yabelson
Comment Utility
i have another problem:how do i print these files afterwards?
0
 
LVL 4

Expert Comment

by:newmang
Comment Utility
You've lost me, what does the code hope to do?
0
 
LVL 3

Expert Comment

by:elcapitan
Comment Utility
>>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--
0
 
LVL 2

Expert Comment

by:obg
Comment Utility
Once again, what are you trying to do? sizeof(file[k]) should probably be strlen(file[k])...
0
 
LVL 10

Expert Comment

by:makerp
Comment Utility
before fprintf should you not open the file and get a handle or FILE pointer ?
0
 
LVL 10

Accepted Solution

by:
makerp earned 30 total points
Comment Utility
i think what you have should be like this, i am dubious about sizeof(file.....), this probably be strlen or _msize

void main()
{
     int k = 0;
     int flag = 10;
     char filename[10];

     while(k < flag)
     {
               /* create the filename */
               sprintf(filename,"file%u.dat",k);
               /* open the file  */
               FILE *f = fopen(filename,"w+");
               if(!f)
               {
                    perror("fopen");
               }
               for(j=0;j<(sizeof(file[k]));j++)
               {
                    /* write the data */
                    fprintf(f,"%d ",file[k][j]);
                    /* close the file */
               }
               fclose(f);

               k++;
          }
     }
}

0
 
LVL 11

Expert Comment

by:griessh
Comment Utility
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
0
 
LVL 10

Expert Comment

by:makerp
Comment Utility
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
0
 
LVL 1

Author Comment

by:yabelson
Comment Utility
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.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 11

Expert Comment

by:griessh
Comment Utility
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
0
 
LVL 1

Author Comment

by:yabelson
Comment Utility
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:-)
0
 
LVL 1

Author Comment

by:yabelson
Comment Utility
what does the function perror() do?
0
 
LVL 4

Expert Comment

by:newmang
Comment Utility
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

0
 
LVL 11

Expert Comment

by:griessh
Comment Utility
yabelson

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

======
Werner
0
 
LVL 1

Author Comment

by:yabelson
Comment Utility
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.
0
 
LVL 10

Expert Comment

by:makerp
Comment Utility
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
0
 
LVL 11

Expert Comment

by:griessh
Comment Utility
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.
0
 
LVL 1

Author Comment

by:yabelson
Comment Utility
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++;
}

0
 
LVL 1

Author Comment

by:yabelson
Comment Utility
it didnt answer my q fully,but it did get me closer to the finish line
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now