Help!!!!

This is a discussion on Help!!!! within the C Programming forums, part of the General Programming Boards category; hey i am trying to write a project that reads in a file and prints to a file. file in ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    46

    Help!!!!

    hey i am trying to write a project that reads in a file and prints to a file. file in is suppose to have a number seed and an integer to say how many to generate.
    then i open the file, generate print and count in one loop then i print the report.
    the trouble is that when i run the program for sum reason it is not able to read the file in.. i am confused and the project is due tomorrow. oh and i printed num1 and num2 to screen just to see what their values are and they are always zero. so it cannont generate anything.

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdbool.h>
    #include <stdlib.h>
    
    bool openFiles(FILE **pFPIn, FILE **pFPOut);
    int read(int* num1, int* num2, FILE *pFPIn);
    int generate(int arr[], int num1, int num2, FILE *pFPOut);  
    void report(int arr[], FILE *pFPOut);
    
    int main(void)
    {
     
       int arr[95];
       int num1, num2;
       bool success;      
       FILE *pFPOut;
       FILE *pFPIn;
    
        if (openFiles (&pFPIn, &pFPOut)) 
      {  
          read(&num1, &num2, pFPIn);
          generate(arr, num1, num2, pFPOut);
      
      
          report(arr, pFPOut);  
         fclose(pFPOut);
       fclose(pFPIn);
    }
       else 
        {  printf("\nThere has to be two integers in file test.txt");
          printf("\n\n");
    } 
    printf("%d%d", num1, num2);
        return 0;
    }
    
    bool openFiles(FILE **pFPOut, FILE **pFPIn)
    {
      
       bool success = false;
    
        if ((*pFPIn = fopen("test.txt", "r")) != NULL)
       {
          if ((*pFPOut = fopen("test.out", "w")) != NULL)
          {
            
            
    
             success = true;   
             
             
          }
          else
         { 
           printf("\nUnable to open \"test.out\" for writing.\n\n");
           fclose(*pFPIn);
         }
      }
      else
          printf("\nUnable to open \"test\" for reading.\n\n");
         
    
    return success;
    }      
    
    int read(int* num1, int* num2, FILE *pFPIn)
    {
      
    
       fscanf(pFPIn,"%d%d", num1, num2);
      
    return ;
    }
    
    int generate(int arr[], int num1, int num2, FILE *pFPOut)
    {
       int i;
       
       
       int temp;  
    
       srand(num1); 
        for (i = 0; i < num2; i++)
       { temp =  rand() % 95 + 32;
       
       fprintf(pFPOut,"%c", temp);
       arr[temp-32]++;
    
       }
        
    
    }
    
    
    
    void report(int arr[], FILE *pFPOut)
    { 
       
       int i;
          
    
       for (i = 32; i < 126; i++)
    {   
    
      fprintf(pFPOut, "'%c' %5d", i, arr[i-32]);
       
          
       } 
       
    
    return ;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Does test.txt have the expected format? Does it open correctly?

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    46
    ya it has two integers and also has the write permissions to open it

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    You have pFPIn and pFPOut switched.
    So you're trying to read from the output file.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    why is read() int and not void?

    I think your use of fscanf in read() is shoddy.
    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

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    And for the record, this is an inappropriate thread title. The title should describe what the thread is all about, and "Help" isn't very helpful.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    True.
    Only the noobiest of noobs posts a thread called "Help!!!"
    "Problem reading from file" would be good.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Also, you shouldn't have a function with a reserved word for a name (read).
    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. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    46
    sorry about the thread title but i dont understand where i have the files switched. because
    pFPIn is assigned to test.txt and pFPOut is assigned to test.out
    and i am trying to scan pFPIn. please help

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MK27 View Post
    Also, you shouldn't have a function with a reserved word for a name (read).
    I agree, although if it was a reserved word, the compiler would moan immediately. The name read is a library function in some OS's - not a reserved word, and as long as you don't (or anything else in the system doesn't) need the library version of read, you can have your own function called read - but it is a bad idea to do that, since sooner or later, some part of your application will want to use the library function, and then you'll end up chasing down and changing all the places where you use your function called the same as the library function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    the arguments to openFiles() in caller and callee.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    46
    well i switched them and the right intergers are assigned to num1 and num2 but i dont understand why

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    You declare it like this at the top:
    Code:
    bool openFiles(FILE **pFPIn, FILE **pFPOut);
    But the actual definition (below main) is:
    Code:
    bool openFiles(FILE **pFPOut, FILE **pFPIn)
    And you call it like this, with the arguments reversed (from the definition) :
    Code:
    openFiles (&pFPIn, &pFPOut)
    So your output file is assigned to your input FILE pointer and vice versa. Therefore you were trying to read from your ouput file.
    Last edited by nucleon; 11-06-2008 at 05:55 PM. Reason: oops

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    46
    thanks for all the help so far...now when i go to report the numbers and characters some of the frequencys are addresses instead of the frequency of the number....

  15. #15
    Registered User
    Join Date
    Nov 2008
    Posts
    33
    Oh man! I knew it was something simple like that. Shoot, gotta watch those orderings, Starvin', lol. Let me know if it worked for you. Well, that takes a lot of stress off my back since I had it right to begin with. Let me know if it worked for you StarvinMarvin.

    -J

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21