ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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)?
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.
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?
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: {
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.