Thread: Need advise

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Right. Read the documentation on fread. What does fread return?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    Quote Originally Posted by laserlight View Post
    Right. Read the documentation on fread. What does fread return?
    Code:
    while(1)
    {
    if (fread(&numbers, sizeof(float), 1, fp) != 1)
    break;
    fscanf(fp,"%f",array);
    printf("%s",array);
    }
    system("pause");
    I have tried this way but i dont get any output

  3. #18
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    This is wrong... in so many ways.

    You're only reading in every other number into array, because fread() grabs everything else and puts it into an unused variable. Then, you try to fscanf() to the entire array, instead of incrementing an array. It should be more like this:

    Code:
    fscanf(fp, "%f", &array[i]);
    where "i" is a variable that is incremented. Bringing us to the next problem: using an infinite loop and breaking is usually poor design, as shown here. If you use this construct, you won't have a counter variable. Edit: And to clarify, you don't need the fread() at all, because fscanf() also returns the amount of parameters successfully read in.

    And potentially the biggest issue, you're trying to read a 4-byte floating point value into an array of 200 1-byte fixed-point values. If you get to this point successfully and without crashing, the data certainly won't be anything useful.

    Finally, you should take a look at these articles:
    - SourceForge.net: Pause console - cpwiki
    - SourceForge.net: Indentation - cpwiki
    and fix your code before posting back.

  4. #19
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    Quote Originally Posted by memcpy View Post
    This is wrong... in so many ways.

    You're only reading in every other number into array, because fread() grabs everything else and puts it into an unused variable. Then, you try to fscanf() to the entire array, instead of incrementing an array. It should be more like this:

    Code:
    fscanf(fp, "%f", &array[i]);
    where "i" is a variable that is incremented. Bringing us to the next problem: using an infinite loop and breaking is usually poor design, as shown here. If you use this construct, you won't have a counter variable. Edit: And to clarify, you don't need the fread() at all, because fscanf() also returns the amount of parameters successfully read in.

    And potentially the biggest issue, you're trying to read a 4-byte floating point value into an array of 200 1-byte fixed-point values. If you get to this point successfully and without crashing, the data certainly won't be anything useful.

    Finally, you should take a look at these articles:
    - SourceForge.net: Pause console - cpwiki
    - SourceForge.net: Indentation - cpwiki
    and fix your code before posting back.
    Code:
    while(!feof(fp))
    {
        fscanf(fp,"%f",&array[i]);
        i++;
    }
    printf("%s \n",array);
    getchar();
    getchar();
    Still doesnt work(no output). So what should i do?

  5. #20
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Get rid of feof(), make the array of floats (not chars), and make sure you initialize i to zero first.

    Code:
    int i = 0;
    float array[100]; 
    while (i < 100) {
        if (fscanf(fp, "%f", &array[i]) != 1)
            break;
        i++; 
    }
    You also need to use a loop to print, and use the right format specifiers:
    Code:
    int j;
    for (j = 0; j < i; j++) { 
        printf("%f, ", array[j]);
    }
    /* I know for() loops are expensive, but it's for demonstration purposes anyway */

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by memcpy
    I know for() loops are expensive, but it's for demonstration purposes anyway
    Err... they aren't, at least no more than the roughly equivalent while loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    Quote Originally Posted by memcpy View Post
    Get rid of feof(), make the array of floats (not chars), and make sure you initialize i to zero first.

    Code:
    int i = 0;
    float array[100]; 
    while (i < 100) {
        if (fscanf(fp, "%f", &array[i]) != 1)
            break;
        i++; 
    }
    You also need to use a loop to print, and use the right format specifiers:
    Code:
    int j;
    for (j = 0; j < i; j++) { 
        printf("%f, ", array[j]);
    }
    /* I know for() loops are expensive, but it's for demonstration purposes anyway */
    Code:
    while(i==100)
    {
    if(fscanf(fp, "%f", &array[i])!=1);
    break;
    i++;
    }
    for(j=0;j<i;j++)
    {
    printf("%f \n",array[j]);
    }
    system("pause");
    
    }
    Still not getting any output
    Last edited by krakatao; 03-11-2012 at 10:27 AM.

  8. #23
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This looks wrong:

    Code:
    while(i==100)
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #24
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Post your code, I have no idea what you're doing. And while you're at it, drop the system("pause") again, and don't put it back.

  10. #25
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    Quote Originally Posted by MK27 View Post
    This looks wrong:

    Code:
    while(i==100)
    Does not work even with this :/
    Code:
    for (i=0; i<=100; i++)

  11. #26
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Stop selectively listening to us and POST YOUR FULL CODE​.

  12. #27
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int main()
    {
    FILE *fp;
    int i=1,k,j;
    float numbers,average=0,now,temp,minimum;
     
        
         
    if ( (fp=fopen("numbers.dat","wb+"))==NULL)
    {
         printf ("No such a file \n");
         system("pause");
         exit(1);
    }
    printf ("Enter number\n");
    printf ("End ctrl+z \n");
     
    while (!feof(stdin))
    {
           
          printf("Enter %d number : \n",i);
          fscanf(stdin, "%f", &numbers);
          if(feof(stdin))
          {
          break;
          }
          fwrite(&numbers, sizeof(float),1,fp);
          i++;
    }
    i=0;
    rewind(fp);
    printf("Numbers you entered : \n");
    while (1)
    {
          if (fread(&numbers, sizeof(float), 1, fp) != 1)
          break;
          printf("%f ", numbers);
          average=average+numbers;
          i++;
    }
    average=average/i;
    system("pause");
    rewind(fp);
    i=0;
    for (i=0; i<=100; i++)
    {
    if(fscanf(fp, "%f", &array[i])!=1)
    break;
    }
    for(j=0;j<i;j++)
    {
    printf("%f",array[j]);
    }
    system("pause");
    
    }
    Last edited by krakatao; 03-11-2012 at 11:08 AM.

  13. #28
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    Quote Originally Posted by memcpy View Post
    Post your code, I have no idea what you're doing. And while you're at it, drop the system("pause") again, and don't put it back.
    When i will finish program i will change system ("pause");

  14. #29
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    • Get rid of ALL the system(pause) commands. Now. Before you do anything else, because you tend to forget and not change issues.
    • array[] doesn't seem to be defined anywhere. Why did you delete it?
    • Why are you scanning in the numbers twice, once with fread() and once with fscanf()? If I understand this correctly, there should be one loop getting numbers and writing them to the file, and one loop reading from the file and printing numbers. Why you have four loops is beyond me.
    • Don't mix fscanf/fprintf API with fread/fwrite API. If you're trying to do formatted I/O, don't use the binary fwrite/fread.
    • Get rid of the while(1) .... break syntax. It's usually a sign of less-planned code.
    • The variables "k", "now", "temp", and "minimum" don't seem to be doing anything. Fix that or delete them.
    • Why check for feof in a loop checking for feof? Not only is this practice non-standard for stdin (and bad practice in general, read this > FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com ), but it's redundant.

    Fix all of this ^^ BEFORE you post back.

  15. #30

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advise for the day.
    By Mario F. in forum General Discussions
    Replies: 8
    Last Post: 04-01-2010, 03:51 PM
  2. Career Advise
    By ggraz in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-23-2008, 03:57 AM
  3. please advise....
    By manny in forum C Programming
    Replies: 2
    Last Post: 06-07-2006, 06:09 AM
  4. Need some advise
    By ILoveVectors in forum C++ Programming
    Replies: 5
    Last Post: 06-21-2005, 09:24 AM
  5. Some Advise..
    By VaSt in forum C++ Programming
    Replies: 3
    Last Post: 06-21-2003, 01:52 PM