Thread: Files problem

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    118

    Files problem

    I am doing a assignment where i have to read the numbers in a file into arrays so if i had 3 on line 1 and 2 and line 2, it would be array[0]=3 and array[1]=2.

    But i am having a problem (im new so it may be a simple question)

    Code:
    #include <stdio.h>
    
    main() {
           FILE *fp;
           int hour;
           
    fp   = fopen("payrates.dat", "r");  /* input file - read mode */
      if (fp == NULL)
      {
        printf("input file not opened");
        
      }
      
      fscanf(fp, "&#37;d\n", hour);
      printf("%d", hour);
    }
    thats my code, im basically trying to just the get first number in the file but its giving me 0.00000 or 53 or a long number. Anyone can help me out??

    edit: i also just realized i have the same topic name as signpost's thread i didnt know until after i made it
    Last edited by bigmac(rexdale); 11-27-2007 at 08:17 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    main() is usually declared as returning an integer. When main() exits, it should specify a return code. If your input file does not open properly, it should probably exit too, instead of falling through to your fscanf().

    Todd

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    thats what my if statement would display though if the file was opened or not. but its goes through and displays whats in the hours variable which is the bottom printf.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    See this online reference: http://www.cppreference.com/stdio/fscanf.html

    What is fscanf() returning?

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    A integer number from the payrates.dat file.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Wrong answer. Put some error checking into your program and repost. Test the return value of fscanf please.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    I get a minus 1 (-1)

    Code:
    #include <stdio.h>
    
    main() {
           FILE *fp;
           int hour, check;
           
    fp   = fopen("payrates.dat", "r");  /* input file - read mode */
      if (fp == NULL)
      {
        printf("input file not opened");
        
      }
      
      check = fscanf(fp, "&#37;d\n", hour);
      fclose(fp);
      printf("%d", check);
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It's not clear from what I'm seeing whether your file is opening correctly. Is it?

    Also: just like scanf, fscanf takes its argument by address, not by value. If your file is opening correctly and not empty, this is almost certainly your difficulty here.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    Ya, i dont think it is because i added a fprint to see if i can print soemthing into the file and it doesn't

    Code:
    #include <stdio.h>
    
    main() {
           FILE *fp;
           int hour, check;
           char line[56];
           
      fp = fopen("payrates.dat", "a");  /* input file - read mode */
      if (fp == NULL)
      {
        printf("input file not opened");
        
      }
      printf("Enter a line: ");
      gets(line);
      fprintf(fp,"&#37;s\n", line);
      check = fscanf(fp, "%d\n", hour);
      fclose(fp);
      printf("%d", check);
    }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bigmac(rexdale) View Post
    Ya, i dont think it is because i added a fprint to see if i can print soemthing into the file and it doesn't
    Well, you should know. Does the line "input file not opened" print? If so, it didn't open. If not, then it did.

    Can you find a "payrates.dat" file in your directory, somewhere? Is your program ending abnormally? (My copy of your program did, because I ignored the compiler warning about line 17.) If so, the output might not get written before the crash. Fix line 17 (you need an ampersand in front of hour) and see what happens.

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    Code:
       #include <stdio.h>
       main() {
          FILE *fp;
          char filename[81];
          double line[9], i = 0;
          fp = fopen("payrates.dat", "r");
          if (fp == NULL)
             printf("Cannot open &#37;s\n", filename);
          else {  
             do {
             i++;      
             fscanf(fp, "%lf\n", &line[i]);
             printf("%.2lf ", line[i]);  
             } while (i != 10);
             /*do {
                printf("Enter a line: ");
                gets(line);
                if (line[0] != '\0')
                   fprintf(fp,"%s\n", line);
             } while (line[0] != '\0');*/
             fclose(fp);
          }
          printf("\n%.2lf", line[3]);
          /*i = 2 * line;*/
       }
    ya the ampersand works, but i need it to store the numbers in the file into an array now
    Last edited by bigmac(rexdale); 11-27-2007 at 10:00 PM.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Read the FAQ on why gets() is bad, REALLY bad.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    > Read the FAQ on why gets() is bad, REALLY bad.
    To be precise: http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    Code:
             fscanf(fp, "&#37;lf\n", &line[i]);
             printf("%.2lf ", line[i]);
    For doubles, one uses %lf in scanf(), but %f in printf()! %lf doesn't exist with printf(). printf() uses %f for both floats and doubles. Confusing, no?

    main() should also return a value. The implicit int rule that you're using is deprecated. Use int main() and add return 0 if you wish as well. There's another FAQ entry on this. http://faq.cprogramming.com/cgi-bin/...&id=1043284376
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem compiling files that store functions
    By tiachopvutru in forum C++ Programming
    Replies: 10
    Last Post: 05-30-2008, 05:42 PM
  2. Problem with Header files.
    By chakra in forum C Programming
    Replies: 5
    Last Post: 05-10-2008, 01:30 PM
  3. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  4. problem with sending files to a class method..
    By utoots in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2003, 01:38 AM
  5. problem with resource files
    By nickeax in forum Windows Programming
    Replies: 1
    Last Post: 03-14-2003, 02:32 AM