0

I'm sorry if this is a dumb question, but I tried many different sources and I'm still not getting what I want. I'm working on a program that reads from a text file, and in the code below, I'm trying to read the first line that will give me the number of resistor color codes were created below it. I'm trying to read the number (n) and simply print it to see that it reads, but I get nothing. It seems so simple, but I can't seem to get it.

`FILE *fpinpt;
FILE *fpoutpt;
FILE *fpnom;
int n, *ptr;
double a, b, c, d, e, f, g, h, i, j, k, l;
fpinpt= fopen("F:\EGR 107\EE\HW 4\resistorInput.txt","r");
fpoutpt= fopen("F:\EGR 107\EE\HW 4\resistorOutput.txt","w");
fpnom= fopen("F:\EGR 107\EE\HW 4\resistorNominal.txt","w");
fscanf(fpinpt,"%d\n",n);
printf("%d",n);
ptr=(int*)calloc(n, sizeof(int));
if (fpinpt==NULL)
{
    printf("Error reading resistor file\n");
    fclose(fpinpt);
}
if (ptr==NULL) printf("Error, memory not allocated\n");

`

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Curtis Negele
  • 15
  • 2
  • 5
  • Add a newline to your `printf` statement - `printf("%d\n", n);`. Standard output is usually *line buffered*, meaning it won't write anything to the console unless the buffer is full, a newline is sent, or `fflush` is called on the output stream. – John Bode Mar 13 '15 at 20:50
  • the checks for successful calls to fopen should be immediately after the call to fopen() Similarily with the calls to calloc() (and family) In most cases if fopen or calloc fails, then open files should be closed and allocated memory passed to free. Note that if the fopen fails then that file is not open so does not need to be closed. If calloc fails, then that memory is not allocated, so no call to free is needed. – user3629249 Mar 13 '15 at 20:53
  • @JohnBode Thanks, however it did not change the result of the problem. – Curtis Negele Mar 13 '15 at 21:38

2 Answers2

2

in your code

fscanf(fpinpt,"%d\n",n);

should be

fscanf(fpinpt,"%d",&n);

Also, always check for the return value of fscanf(), fopen() to ensure proper input/ operation.

That said, you don't need to cast the return value of malloc() and family.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • I had that earlier, the latest example I tried referencing did that, doesn't work either way. The calloc() is just planning for the future, are you saying to have it return n? – Curtis Negele Mar 13 '15 at 20:41
  • @CurtisNegele look at my answer, your code is a mess, you need to be sure that files opened otherwise everything will go wrong. – Iharob Al Asimi Mar 13 '15 at 20:42
  • @CurtisNegele are you sure all the `fopen()`s succeeded? – Sourav Ghosh Mar 13 '15 at 20:43
  • @SouravGhosh none of them did, the ``\`` are not escaped. – Iharob Al Asimi Mar 13 '15 at 20:44
  • @iharob that's why (as I mentioned) we should always check for the return value of library functions. :-) – Sourav Ghosh Mar 13 '15 at 20:45
  • 1
    @SouravGhosh you always do that's why I like your answers. – Iharob Al Asimi Mar 13 '15 at 20:46
  • I do have an error check to see if the opened file gave content to fpinpt, any suggestions? Instead of returning it, I'm just trying to print it so I can see it, later I would take that out. Is this not an effective way to check the number assigned to n? – Curtis Negele Mar 13 '15 at 20:57
  • 1
    @CurtisNegele `if (fpinpt==NULL)` check should be before using `fpinpt`. – Sourav Ghosh Mar 13 '15 at 20:59
  • @SouravGhosh, moved it, no change in result however. – Curtis Negele Mar 13 '15 at 21:01
  • @CurtisNegele what is the value of `n` printed by printf? – Sourav Ghosh Mar 13 '15 at 21:04
  • @SouravGhosh Nothing prints, which is my problem. It should read the first line, which is a whole number. – Curtis Negele Mar 13 '15 at 21:09
  • @CurtisNegele eh?? did you check the return value of `fscanf()`? did it return 1? if so, can you add a `\n` in the printf like `printf("%d\n",n);` and try? – Sourav Ghosh Mar 13 '15 at 21:11
  • @SouravGhosh Nothing returns, the application does not end, window remains blank, and I have to force close it from running in the backround after exiting. – Curtis Negele Mar 13 '15 at 21:15
  • @CurtisNegele is it? can you please provide a [MCVE](http://stackoverflow.com/help/mcve)? – Sourav Ghosh Mar 13 '15 at 21:24
  • Hopefully this is satisfactory? I apologize for my noob-ness. `FILE *fpinpt;` `int n;` `fpinpt= fopen("F:\\EGR 107\\EE\\HW 4\\resistorInput.txt","r");` `if (fpinpt==NULL) printf("Error reading resistor file\n");` `fscanf(fpinpt,"%d\n",&n);` `printf("%d\n",n);` – Curtis Negele Mar 13 '15 at 21:34
  • @CurtisNegele nah. use something like `if ( fscanf(fpinpt,"%d",&n) != 1) return; printf("%d\n",n);` – Sourav Ghosh Mar 13 '15 at 21:42
  • @SouravGhosh Function compiles and runs, still get an empty box. – Curtis Negele Mar 13 '15 at 21:48
  • @CurtisNegele what is the contains of resistorInput.txt? – Sourav Ghosh Mar 13 '15 at 21:50
  • @SouravGhosh It is a texst file created by an application which creates a random list of resistor codes. The first line is the number of resistor codes created (ie: 778). That's what I'm trying to test for reading first. Also getting the number will allow for a loop to get all of the resistor codes (ie: ICGH\n BIACB\n CCDKA\n(\n is not visible to user, indicates a new line)) The letters correspond to different band colors. – Curtis Negele Mar 13 '15 at 21:56
0

The main cause of problems is that you never check for errors. For every fopen() there must be a

if (fopenedFile == NULL)
    ohNoMust_ICannotUse_fopenedFile();

now to why are your fopen()'s failing it's because you haven't escaped the '\' character, so each file name should fix it like this

"F:\\EGR 107\\EE\\HW 4\\resistorInput.txt"

Then, files will be opened, but fscanf() will not work because of what the other answer already ("while I was editing mine") addressed.

NOTE:

Please read both answers, it doesn't make sense to repeat what @SouravGhosh already said.

Community
  • 1
  • 1
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97