1

I am testing to make sure that my file read and my conversion to uppercase is working properly, but when I print the array, the first letter in the array does not show up in the output. Here's the function in question:

int processFile(){
  int i;
  i = 0;
  if (!(fp = fopen("congress.txt", "r"))) {
    printf("congress.txt could not be opened for input.");
    exit(1);
  }

  while (!feof(fp)){
    fscanf(fp, "%c", &origFile[i]);
    i++;
  }

  for (i = 0; i <= SIZE; i++){
    if (origFile[i] >= 'a' && origFile[i] <= 'z') upperFile[i] = origFile[i] -= 32;
  }

  for(i = 0; i <= SIZE; i++){
    printf("%c",upperFile[i]);
  }
}
ernd enson
  • 1,764
  • 1
  • 14
  • 29
Pahjay
  • 17
  • 6
  • 1
    What is the output you get? – shree.pat18 Apr 10 '14 at 02:46
  • 3
    You never assign anything to `upperFile[i]` if the value in the original is *not* lower case (but you still skip the slot). I.e. "AFileName.txt" will skip the A, F, N, and `.` slots of `upperFile`, leaving whatever junk was there prior. Related: [this `while (!feof(fp))` is a bad idea](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). – WhozCraig Apr 10 '14 at 02:47
  • @WhozCraig Do you mind referring to a resource to explain why it's a bad idea or elaborating? I am only a month or so into learning C and I would like to know more. – Pahjay Apr 10 '14 at 02:51
  • Hover over the statement in the comment. Its a link to why I said that. And if you're only a month in and learn this now, tis good, as it will save you much-headache in the future. [Read the linked question and top-answer](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). – WhozCraig Apr 10 '14 at 02:52
  • @WhozCraig Oh I don't know how I missed that link. Thank you! – Pahjay Apr 10 '14 at 02:57

1 Answers1

3

I'm guessing the the first character in congress.txt is an uppercase character. If you look at the line:

 if (origFile[i] >= 'a' && origFile[i] <= 'z') upperFile[i] = origFile[i] -= 32;

all uppercase characters are being skipped and not written into upperFile. You may notice in your output that all the first letters of sentences are missing.

Phillip Ngan
  • 15,482
  • 8
  • 63
  • 79