Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dougphd

macrumors member
Original poster
Apr 28, 2016
70
0
The following code opens up a list of filenames but won't open those files. Running the code shows the filename to be correct and produces an errno of 0. Permissions should allow the file to be open.
Douglass-MBP:country_data douglasbrenner$ ls -l ECHW.TXT
-rw-rw-rw-@ 1 douglasbrenner staff 17348 Aug 14 07:52 ECHW.TXT
The file can be opened elsewhere.

The list and first file are attached.

Code:
#define list_of_etfs "/Users/douglasbrenner/ANDY/ct/country_data/etfs.txt"

#define data_dir  "/Users/douglasbrenner/ANDY/ct/country_data/"

externint errno;


int main(int argc, constchar * argv[])

{

   

    FILE  *etfs, *ETF;

    char etf[15], filename[150];

    structetf_data data;

    int i,num_of_points;

    int errnum;

   

    char line[200];

  //open list

    if ( (etfs = fopen(list_of_etfs, "r")) == NULL)

        printf("list not open\n");

// read etfs
   

      while ((fgets(etf,sizeof(etf), etfs)) != NULL )

        {

            printf("%s\n",etf);

            strcpy(filename,data_dir);

            strcat(filename,etf);

            if   ((ETF = fopen(filename,"r")) == NULL)

            {

                printf("%s file not opened, errnum %d\n ", filename, errnum);

                fprintf(stderr, "%s\n ", strerror(errnum));

                exit(0);

            }

               

            else

              printf("file opened\n");



            fclose(ETF);

    

        }

    fclose(etfs);
/[code]
 

Attachments

  • ECHW.TXT
    16.9 KB · Views: 270
  • etfs.txt
    290 bytes · Views: 270

dylanryan

macrumors member
Aug 21, 2011
57
2
From man fgets: "The newline, if any, is retained". So, you are looking for a file path like:

/Users/douglasbrenner/ANDY/ct/country_data/ECHW.TXT\n

and not finding it because you don't have any files named that (note the trailing newline). What you actually have is

/Users/douglasbrenner/ANDY/ct/country_data/ECHW.TXT

with no trailing newline.



As for "produces an errno of 0.", you are printing your local variable errnum which is never initialized, so it could be anything. Sheer luck it is 0. Try using the extern int errno instead. That is what is set by these functions.
 

dougphd

macrumors member
Original poster
Apr 28, 2016
70
0
Thanks. I though it was something along those lines but I didn't understand why etfs.txt opened and ECHW.TXT didn't. It opened because I defined it in the header. I also had tried fscanf but that failed too. I just tried it again and it worked. Must have screwed up there before. I think this time this problem will sink to to my brain.
 

dylanryan

macrumors member
Aug 21, 2011
57
2
No problem. What I usually do when I want to print out a value and need to make sure there are no trailing spaces or newlines or other things that are hard to spot, I use printf("[%s]\n",...). In this case, that would have shown

[/path/goes/here
]

which would have been obvious. This also works if you are concerned about trailing spaces, and so on.
 

robvas

macrumors 68040
Mar 29, 2009
3,240
629
USA
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#define list_of_etfs "/Users/robert/Downloads/doug/etfs.txt"

#define data_dir "/Users/robert/Downloads/doug/"

extern int errno;

int main(int argc, const char * argv[]) {
  FILE *etfs, *ETF;
  char etf[15], filename[150];
  char line[200];

  //open list
  etfs = fopen(list_of_etfs, "r");
  if (etfs == NULL) {
    printf("list not open\n");
    exit(0);
  } else {
      printf("Successfully opened list of etfs");
  }
  // read etfs
  while ((fgets(etf, sizeof(etf), etfs)) != NULL)  {
    //build our new file name
    strcpy(filename, data_dir);
    strcat(filename, etf);
    strtok(filename, "\n");
    printf("\n\nRead in: %s from list of etfs\n", etf);

    ETF = fopen(filename, "r");
    if (ETF == NULL) {
      printf("%s file not opened, errno %d\n ", filename, errno);
      fprintf(stderr, "%s\n ", strerror(errno));

      exit(0);
    } else {
        printf("Successfully opened file %s\n", filename);
    }
    fclose(ETF);
  }
  fclose(etfs);
}

Fixed a bunch of other stuff for you too.
[doublepost=1471197833][/doublepost]You gotta learn Python doug

Code:
#this is a comment

#this is like an 'include' in C
import sys

list_of_etfs = "/Users/robert/Downloads/doug/etfs.txt"
data_dir = "/Users/robert/Downloads/doug/"

list_file = open(list_of_etfs)

for line in list_file:
    filename = line
    next_file = open(data_dir + filename.rstrip('\n')) # remove the \n like in C
    for l in next_file:
        sys.stdout.write(l)
Save you so much time and effort instead of trying to bash your head around handling strings and such in C
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.