fopen() - don't know what I'm doing wrong


 
Thread Tools Search this Thread
Top Forums Programming fopen() - don't know what I'm doing wrong
# 1  
Old 04-30-2009
fopen() - don't know what I'm doing wrong

This code works fine when I use a command line argument for fopen()'s parameter, but when I change it to a filename, the program freezes upon compilation. input.txt is definitely there, so I can't figure it out. Thanks.

Code:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

int main(int argc, char **argv)
{
    int arr[255]={0};
    int i=0;
    FILE *text=fopen("input.txt", "r");
    
    while ((i = getchar()) != EOF)
        arr[i]++;
    fclose(text);
    for(i='0'; i<'z'; i++)
    {
           if(isalnum(i) )
            fprintf(stdout, "The number of '%c' is %d\n", i, arr[i]);
    }
    
    return 0;
}

# 2  
Old 04-30-2009
what do you mean freeze?

the program is waiting for input followed by EOF - ctrl+d on most systems. the text file is not used at all.


you should really check to see if the fopen succeded.

ie:

Code:
    
    FILE *text = NULL;

    text = fopen("input.txt", "r");

    if(text == NULL)
    {
       printf("ERROR: could not open file\n");
       exit(1);
    }

# 3  
Old 05-01-2009
Quote:
Originally Posted by frank_rizzo
what do you mean freeze?

the program is waiting for input followed by EOF - ctrl+d on most systems. the text file is not used at all.


you should really check to see if the fopen succeded.

ie:

Code:
    
    FILE *text = NULL;

    text = fopen("input.txt", "r");

    if(text == NULL)
    {
       printf("ERROR: could not open file\n");
       exit(1);
    }

Thanks! I changed some stuff around and got it to work.

I'm having trouble now, again, with writing the output to both the screen and appending the same text file... it successfully appends to it, but not the same output that was outputted to the screen (every occurrence is counted as zero):

Code:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

int main(int argc, char **argv)
{
    pid_t pID = fork();
    
    if (pID == 0) {    //child
      int y[255]={0}, x=0;
       FILE *text=fopen("input.txt", "r");
    
       while ((x = fgetc(text)) != EOF)
           y[x]++;
       fclose(text);
       for(x='0'; x<'z'; x++)
       {
              if(isalnum(x) )
               fprintf(stdout, "The number of '%c' is %d\n", x, y[x]);
       }
       
    }
     else if (pID < 0)            // failed to fork
    {
        exit(1);
    }
        else                                   // parent
    {
      
       int y[255]={0}, x=0;
       FILE *text=fopen("input.txt", "a");
    
       while ((x = fgetc(text)) != EOF)
           y[x]++;
              
       for(x='0'; x<'z'; x++)
       {
              if(isalnum(x) )
               fprintf(text, "The number of '%c' is %d\n", x, y[x]);
       }
     fclose(text);
    }
    
    return 0;
}

# 4  
Old 05-01-2009
You can't read from a file opened for append. Try opening it with "a+" instead of "a", that will let you read from the beginning of the file and write to the end.

You really should set your array to zero before you start counting by the way. Your code is only guaranteed to set the first element to zero before it starts counting, the rest may in fact contain undefined values until you set them to zero.

Lastly, you should realize that with two independent processes, they may produce unpredictable results when operating on the same file, i.e. the child process may hit EOF before the parent process finishes appending, or it may not, all depending on which one just happens to get CPU time from the kernel first and when stdio decides to flush buffered output and other random factors.

Last edited by Corona688; 05-01-2009 at 12:28 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Programming

help plz - fopen()

Hello, I have a problem here, I want to write a function called"myfopen()" instead of "fopen()" for writing this function I must not use the <stdio.h> library, Can you help me? thanks a lot (2 Replies)
Discussion started by: hamed.samie
2 Replies

2. Programming

overhead of fopen/freopen

I always assumed the fopen/freopen is very costly, so when I needed to work with many files within on process I spent extra time to implement a list of FILE * pointers to avoid extra open/reopen but it did not produced any better results. Here is a task at hand - there is a huge stream of data... (4 Replies)
Discussion started by: migurus
4 Replies

3. Programming

fopen and open

what is the difference between fopen and open fread and read fwrite and write open and create why this much of functions for the i/o when everything does the same...? What is their major difference? In which case, which is the best to use. :confused:'ed Collins (2 Replies)
Discussion started by: collins
2 Replies

4. UNIX for Advanced & Expert Users

Linux fopen() mistery. Help required.

Hello! I'm having problems with fopen() call in Linux. I have shared library (created by myself) that implements some file operations: int lib_func(char* file_name) { ... fd = fopen(file_name, "r"); if(!fd) {... exit with error ...} ... do something useful using fd ... ... (2 Replies)
Discussion started by: kalbi
2 Replies

5. Web Development

CAN TCPDF USE fopen() or Convert URL To PDF?

Dear all, I'm a newbie for PHP and TCPDF ,I have to change the URL to PDF, so I used FPDF , But it cannot convert most of the advanced HTML tags. So explored again and found TCPDF , it can do most of the tag but I cannot found to change URL to PDF. So Does anyone can point the example... (0 Replies)
Discussion started by: athae
0 Replies

6. Programming

fopen() + reading in large text files

For reading in large text files (say files over 1kB in size) are there any issues with fopen() that I should be aware of ? cheers (2 Replies)
Discussion started by: JamesGoh
2 Replies

7. Programming

.cc fopen failed - Broken Pipe

hello.. i make some code with C in freebsd 5.4 and compile it in solaris somehow i succeed compile the program. but when i run it, i got error message "Broken Pipe" i looked out the syntax that that caused this, fp = fopen("file.tmp","r"); does anyone know why, and how to solve this... (3 Replies)
Discussion started by: kuampang
3 Replies

8. Programming

Mac OS X - open() and fopen() with French filename

Hi I was trying to open a file with french name on Mac OS-X with open() and fopen() but it didn't work.Do we have any POSIX unix func. which can be used to open any file with special name. if anybody has an idea plz help. Thanks Mohit (1 Reply)
Discussion started by: mohit grover
1 Replies
Login or Register to Ask a Question