LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-22-2013, 07:06 PM   #1
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Rep: Reputation: Disabled
problem with fopen() "rb" param


Wondering if there is any reason why my clang compiler is unable to open a file "myFile.txt" with the parameter
"rb". The file is in the same directory as my code, so I don't know what else could be wrong. Code is written in "C", and comes from a text.
 
Old 11-22-2013, 07:10 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by atlantis43 View Post
Wondering if there is any reason why my clang compiler is unable to open a file "myFile.txt" with the parameter
"rb". The file is in the same directory as my code, so I don't know what else could be wrong. Code is written in "C", and comes from a text.
Perhaps it is "clang"? Why not use a proven compiler such as GCC (err, 'gcc' for C source code)?
 
Old 11-22-2013, 07:24 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by atlantis43 View Post
The file is in the same directory as my code, so I don't know what else could be wrong.
The directory where the code lives doesn't matter, what matters is what directory you are in when calling the executable.

When you say "unable to open" does that mean fopen() returns NULL? Did you check errno (use perror(3) to translate the number to a message)?
 
Old 11-22-2013, 07:48 PM   #4
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Yes, returning NULL, although the file does have text within. I further realized that there must be something all fouled, since I cant even open it in "r" or "a" params.
I'll have to check it further to see if there's something more obvious that's the cause of my problem.
 
Old 11-22-2013, 07:54 PM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by atlantis43 View Post
Yes, returning NULL, although the file does have text within. I further realized that there must be something all fouled, since I cant even open it in "r" or "a" params.
I'll have to check it further to see if there's something more obvious that's the cause of my problem.
Hey-sus!!! Do you have the appropriate permissions to open the file? This is Linux right, not windows?
 
Old 11-22-2013, 08:15 PM   #6
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Quote:
Hey-sus!!! Do you have the appropriate permissions to open the file? This is Linux right, not windows?
Yup, Linux.
No problems with any other File I/O code examples, so I haven't figured out what's wrong yet. Also tried gcc,as suggested earlier, but no benefit. Must be something wrong with my 'clear buffer' code.
Code is as follows, so perhaps someone can relieve my confusion.
Code:
/* reverse.c -- displays a file in reverse order */
#include <stdio.h>
#include <stdlib.h>
#define CNTL_Z '\032'   /* eof marker in DOS text files */
#define SLEN 50
int main(void)
{
    char file[SLEN];
    char ch;
    FILE *fp;
    long count, last;

    puts("Enter the name of the file to be processed:");
    fgets(file,50,stdin);
    while (fgets(file,50,stdin) != NULL && file[0] != '\n')
    {
if ((fp = fopen(file,"rb")) == NULL)
    {                      /* read-only and binary modes */
        printf("reverse can't open %s\n", file);
        exit(1);
    }
    }
    fseek(fp, 0L, SEEK_END);        /* go to end of file */
    last = ftell(fp);
/* if SEEK_END not supported, use this instead           */
/*  last = 0;
    while (getc(fp) != EOF)
        last++;
*/
    for (count = last- 1; count >= 0; count--)
    {
        fseek(fp, count, SEEK_SET); /* go backward       */
        ch = getc(fp);
    /* for DOS, works with UNIX */
        if (ch != CNTL_Z && ch != '\r')
            putchar(ch);
    /* for Macintosh            */
    /*  if (ch == '\r')
            putchar('\n');
         else
             putchar(ch)
    */
   }
   putchar('\n');
   fclose(fp);
   
   return 0;
}

Last edited by atlantis43; 11-22-2013 at 08:29 PM. Reason: {
 
Old 11-22-2013, 09:15 PM   #7
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Quote:
Perhaps it is "clang"? Why not use a proven compiler such as GCC (err, 'gcc' for C source code)?
Just found that you were correct: it does compile with gcc (all error handling excluded) if I leave the code as original, which used gets(), which I tried to convert to fgets(). Clearly a problem with my code for clearing '\n'from buffer.
 
Old 11-22-2013, 11:09 PM   #8
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,781

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
Quote:
Originally Posted by atlantis43 View Post
Code:
if ((fp = fopen(file,"rb")) == NULL)
    {                      /* read-only and binary modes */
        printf("reverse can't open %s\n", file);
        exit(1);
    }
    }
Replace that printf() with
Code:
        perror(file);
so that you get to see what actually failed.
 
1 members found this post helpful.
Old 11-23-2013, 12:27 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Compilers don't open files (except for source/object files), libc does which is compiler-independent. Fix your program.

Code:
if (! fgets (buff, sizeof (buff), stdin)) {
    /* read error */
}
len= strlen (buff);
if (len>0 && buff[len-1]=='\n') buff[--len]= '\0';
 
1 members found this post helpful.
Old 11-23-2013, 06:19 AM   #10
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Or just:
Code:
if (! fgets (buff, sizeof (buff), stdin)) {
    /* read error */
}
char* nl = strchr(buff, '\n');
if (nl)
{
    *nl = '\0';
}
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem understanding IPtables "-d" param resetreset Linux - Networking 4 04-16-2012 03:46 AM
"Too many open files in system" PHP fopen() MicahCarrick Programming 5 12-20-2007 08:40 PM
Segfault on fopen("./tmplp","w"); debiant Programming 3 09-07-2006 11:09 PM
Fatal error @ "fopen(stage1.c:383) failed: ) I/O error". Tripitaka Linux - Newbie 7 02-05-2006 05:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration