-3

I can see the last printf output of y but the fpc turns null.

I suspected for double quotes in fopen function but not could not find a solution: how to fix it?

Part of the code ;

char *y = &arr_lines[1024*2];

FILE *fpc = fopen(y, "r");

        if (fpc == NULL) {
                printf("Error opening file.\n");
                //return -1;
        }
printf("TEST %s\n",y);

When I run the code;

Error opening file.
TEST /Users/lessons/AbstractLesson.java

Here is the full code;

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define LINESIZE 1024
int main(void){

    char *arr_lines, *line;
    char buf_line[LINESIZE];
    int num_lines = 0;
    char buf[10240];

    // open file
    FILE *fp = fopen("/tmp/file", "r");
    //FILE *fp1 = fopen(arr_lines, "r");
    if (fp == NULL) {
            printf("Error opening file.\n");
            return -1;
    }
    // get number of lines; from http://stackoverflow.com/a/3837983
    while (fgets(buf_line, LINESIZE, fp))
            if (!(strlen(buf_line) == LINESIZE-1 && buf_line[LINESIZE-2] != '\n'))
                    num_lines++;
    // allocate memory
    arr_lines = (char*)malloc(num_lines * 1024 * sizeof(char));
    // read lines
    rewind(fp);
    num_lines = 0;
    line=arr_lines;
    while (fgets(line, LINESIZE, fp))
            if (!(strlen(line) == LINESIZE-1 && line[LINESIZE-2] != '\n'))
                    line +=  LINESIZE;
    // print first four lines
    char *y = &arr_lines[1024*2];

    FILE *fpc = fopen(y, "r");
        //FILE *fp1 = fopen(arr_lines, "r");
        if (fpc == NULL) {
                printf("Error opening file.\n");
                //return -1;
        }

printf("TEST [%s]\n",y);
     //x = &arr_lines[1024*0];
    // y = *x;

  // finish
    fclose(fp);
    return 0;

}

Scott
  • 3
  • 1
  • 1
  • 4
  • 1
    Does `/Users/lessons/AbstractLesson.java` exist? Is it readable? – NPE Dec 26 '14 at 21:37
  • Have you confirmed that the file `/Users/lessons/AbstractLesson.java` exists and do you have read access to it? – lurker Dec 26 '14 at 21:37
  • yes file exists and its readable – Scott Dec 26 '14 at 21:39
  • 1
    Please include the exact output of `ls -la /Users/lessons/AbstractLesson.java` in your question. – NPE Dec 26 '14 at 21:39
  • 2
    What is the definition of `arr_lines` and how are you setting it? – Dai Dec 26 '14 at 21:39
  • 1
    You could inspect the error number to learn more about the actual problem: http://man7.org/linux/man-pages/man3/errno.3.html – Jasper Dec 26 '14 at 21:40
  • Also, please change the `printf` to `printf("TEST [%s]\n",y);` and include the output, so that we can be sure that there are no trailing whitespaces or another anomalies in the filename. – NPE Dec 26 '14 at 21:41
  • What does `errno` or `perror()` say? – John C Dec 26 '14 at 21:41
  • Change `printf("TEST %s\n",y)` to `printf("TEST \"%s\"\n",y)` so we can see if you have any extra spaces in the filename. – Jonathan Wood Dec 26 '14 at 21:45

2 Answers2

1

Change printf("TEST %s\n", y) to printf("TEST \"%s\"\n", y) so we can see if you have any extra whitespace characters in the filename.

fgets() returns the new line, if it's there. I didn't see where your code clears the newline. Does your path string include the new line?

Beyond that, fopen() is almost certainly working correctly. The only options are 1) The path is not correct, 2) the path has whitespace or other invalid characters, or 3) the file is not available for reading.

If you don't have a new line in your path, then you simply haven't provided enough information to resolve this issue.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • Error opening file. TEST "/Users/Paz/Downloads/Web//WEBINF/classes/org/test/webx/lessons/AbstractLesson.java" – Scott Dec 26 '14 at 21:57
  • the output is the same with printf("TEST \"%s\"\n", y) – Scott Dec 26 '14 at 21:59
  • Is this Windows? Did you call `GetLastError()` or `erno` to determine the cause? – Jonathan Wood Dec 26 '14 at 21:59
  • Then either the path is not correct, or the file is not available for reading. – Jonathan Wood Dec 26 '14 at 22:00
  • the reason was the newline char. thanks guys i fixed it. – Scott Dec 26 '14 at 22:26
  • @Scott: So my original comment to your question 45 minutes ago to add quotes to your test printf call would've resolved this. Welcome to Stackoverflow. You may get a lot of replies to your questions so you should only post when you can actively try the suggestions given to you. And be sure to accept that answer that was helpful. – Jonathan Wood Dec 26 '14 at 22:30
0

I suspect the path, /Users/lessons/AbstractLesson.java, is wrong. It looks like an OSX path and it might be missing the username between Users and lessons.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 1
    @Scott: I don't understand your last comment. – NPE Dec 26 '14 at 21:42
  • I deliberately cut the output for confidentiality reasons.. Do not care about the path , its correct in reality – Scott Dec 26 '14 at 21:44
  • @Scott: Oh, you're showing an *approximation* of what's actually happening. I don't think that's a good approach given the nature of the problem. I made some suggestions in the comments to the question. Good luck. – NPE Dec 26 '14 at 21:46
  • 1
    @Scott: Why am I having trouble believing you about the path being correct? For starters, `fopen()` is well tested and it works. Second, there are many comments asking you to provide more details about the file and you haven't responded to them. The only other alternative is that you have bogus characters as part of your filename, but you haven't provided sufficient information for anyone to determine that. – Jonathan Wood Dec 26 '14 at 21:47
  • @Scott: Make sure `y` doesn't have a trailing newline. – NPE Dec 26 '14 at 21:52
  • @Scott: According to another comment of yours, *"the reason was the newline char. thanks guys i fixed it."* This could have been caught by the following suggestion made quite some time ago: *please change the `printf` to `printf("TEST [%s]\n",y)`;*. You've made it remarkably hard for us to help you. – NPE Dec 26 '14 at 22:33