English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

#include
#include

int main(void) {
//user information
printf("This programme counts the number of each letter in passage.txt.");

//define file and arrays.
FILE *FileReference;
char Letter[2]= {0}, ArrayOfLetters[128]= {0};
int i=65;

//open file
FileReference=fopen("passage.txt", "r");
if (FileReference == NULL) {
printf("File did not open correctly\n");
return 1;
}

//Loop for capital letters
for (i=65; i<=90; i++) {
while (fgets(Letter,2,stdin) !=0) {
ArrayOfLetters[(Letter[2])]++;
}
}

printf("\n\n%d=%s", ArrayOfLetters, ArrayOfLetters);
return 0;
}

2006-11-27 20:09:46 · 6 answers · asked by sarciness 3 in Computers & Internet Programming & Design

Changed stdin to FileReference. Still doesn't work.

2006-11-27 20:15:51 · update #1

The program compiles... but does not print the array I'm trying to print. I don't know if Letter is incrementing the array or not.

2006-11-27 20:26:24 · update #2

I think my fopen is fine, but I can't actually read what you guys have said as Yahoo! seems to put in ... where I need details!

2006-11-27 20:31:28 · update #3

Paul B- I think that's what my program is trying to do- I'm trying to work out why it doesn't.

2006-11-27 20:37:15 · update #4

6 answers

FileReference=fopen("passage.t... "r");

should be

FileReference=fopen("passage.txt" , "r");

fopen takes two arguments.

2006-11-27 20:17:55 · answer #1 · answered by Simon E 2 · 0 1

FileReference=fopen("passage.t... "r");
Shouldn't that be FileReference=fopen("passage.txt", "r");

There is an easier way to do this: If you are expecting ascii then you can create an array of 256 elements of int then get the acsii code of each char in the file and increment the element at the position in the array that matches the ascii code of the char.
I'm not a C programmer but the concept should work.

2006-11-28 04:19:38 · answer #2 · answered by Paul B 3 · 0 0

Try this - at least it works via my old Watcom compiler


#include
#include
#include

int main(void)
{
//user information
printf("This programme counts the number of each letter in passage.txt.\n\n");

//define file and arrays.
FILE *FileReference;
int LetterCount[26];
int i;
int c;

for (i=0; i<26; i++)
{
LetterCount[i] = 0;
}

//open file
FileReference=freopen("passage.txt", "r", stdin);
if (FileReference == NULL)
{
printf("File did not open correctly\n");
printf("Press any key\n\n");
getch(); // So window does not disappear via Windows
return 1;
}

while( (c = getchar()) != EOF )
{
//Loop for capital letters
for (i=65; i<=90; i++)
{
if (i == c) LetterCount[i - 65]++;
}
}
fclose (FileReference);

for (i=0; i<26; i++)
{
printf (" %c %d\n", i + 65, LetterCount[i]);
}
printf("\nPress any key\n");
getch(); // So window does not disappear via Windows
return 0;
}

2006-11-28 10:29:17 · answer #3 · answered by ROY L 6 · 0 0

you need to clear your ArrayOfLetters[128] array. you are setting the last to 0 (string terminator) which is fine, but every other position has garbage in it. at the beginning of your program, add
for (int a = 0; a < 128; a++)
ArrayOfLetters[a] = 0;

2006-11-28 09:16:57 · answer #4 · answered by justme 7 · 0 0

Presumably you know at what point it stops working, and what error code is being returned to you. Care to share?

2006-11-28 04:21:43 · answer #5 · answered by Anonymous · 0 0

please reiterate on exactly what compilation errors you are getting if any.

2006-11-28 04:51:41 · answer #6 · answered by Adeel I 3 · 0 0

fedest.com, questions and answers