ignoring # when reading from text file [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have the following text file
#1234
5,4
#tfxc
01AAX
11AA1
#tfxc
11AA1
11111
And I want to ignore the # and the data behind it when reading from the text file and the 5,4 are the dimensions of the matrix which I am storing in a 2d array.
This is my code:
#include <stdio.h>
FILE *inp;
int main(void) {
int i, j;
int y = 0;
int x = 0;
char comma;
char arr = 0;
inp = fopen("App.txt", "r");
fscanf(inp, "%d", &x);
fscanf(inp, "%c", &comma);
fscanf(inp, "%d", &y);
char array[y][x];
for (i = 0; i < y; i++) {
for (j = 0; j < x; j++) {
fscanf(inp, "%c", &arr);
if ((arr == '1') || (arr == 'X') || (arr == '0') || (arr == 'A')) {
array[i][j] = arr;
} else {
j--;
}
printf("%c", arr);
}
}
}
How I can do this?

Use fgets() to read lines, check if the line should be ignored, else parse the line to initialize your matrix.
Here is an example:
#include <stdio.h>
int main(void) {
int c, i, j, k, x = 0, y = 0;
char line[1024];
FILE *inp;
inp = fopen("App.txt", "r");
if (inp == NULL) {
printf("cannot open App.txt\n");
exit(1);
}
while (fgets(line, sizeof line, fp)) {
if (line[0] == '#') continue;
if (sscanf(line, "%d,%d", &x, &y) == 2)
break;
printf("invalid line: %s", line);
exit(1);
}
char array[y][x];
for (i = 0; i < y;) {
if (!fgets(line, sizeof line, inp)) {
printf("missing matrix data at row %d\n", i);
break;
}
if (line[0] == '#')
continue;
for (j = k = 0; j < x && line[k] != '\0'; k++) {
c = line[k];
if (c == '1' || c == 'X' || c == '0' || c == 'A') {
array[i][j] = c;
j++;
putchar(c);
}
}
putchar('\n');
if (j != x) {
printf("missing matrix values at %d,%d\n", i, j);
}
i++;
}
fclose(inp);
return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
int i, j;
int y = 0, x = 0;
FILE *inp = fopen("App.txt", "r");
if(!inp){
perror("fopen\n");
exit(EXIT_FAILURE);
}
while(2 != fscanf(inp, "%d,%d", &x, &y)){
int ch;
while((ch=fgetc(inp)) != '\n' && ch !=EOF);//skip
if(ch == EOF){
fprintf(stderr, "There is no dimension specified.\n");
exit(EXIT_FAILURE);
}
}
char array[y][x];
char format[32];
sprintf(format, "%%%d[01AX]%%c", x);
for (i = 0; i < y; i++) {
int status;
char newline = 0, buff[x+1];
status = fscanf(inp, format, buff, &newline);
if(status == 0 || status == 2 && newline != '\n'){//status 1 is OK
int ch;
while((ch=fgetc(inp)) != '\n' && ch !=EOF);//skip
--i;
continue;
}
if(status == EOF){
fprintf(stderr, "Necessary data is missing.\n");
exit(EXIT_FAILURE);
}
memcpy(array[i], buff, x);
}
fclose(inp);
//check print
for (i = 0; i < y; i++) {
for (j = 0; j < x; j++) {
putchar(array[i][j]);
}
putchar('\n');
}
return 0;
}

Related

The file becomes NULL after printing 254 rows in .txt

I am trying to solve the problem of recursion.
The problem is : Given a number n, print out all the possible combinations of set {a, b, ..., a+n-1}. Output the answer in "output-n.txt". 1<=n<=9.
Here's my code:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
void comb(int n,int curlen,int j,int num[],int flag) {
int i,k;
char filename[20] = "output-0.txt";
filename[7] = n + '0';
char ans[20] = { 0 };
FILE* fptr;
fptr = fopen(filename, "a");
if (fptr != NULL) {
if (curlen < n && flag != 0) {
for (i = 0; num[i] != 0; i++) {
ans[i] = num[i] + '`';
}
for (k = 0; k <= curlen; k++) {
fprintf(fptr, "%c", ans[k]);
}
fprintf(fptr, " ");
if (curlen == 0 && num[0] == n) {
fclose(fptr);
}
else {
comb(n, curlen + 1, j + 1, num, 0);
}
}
else {
for (; j <= n; j++) {
num[curlen] = j;
comb(n, curlen, j, num, 1);
}
}
}
}
int main() {
int n,flag=0,num[20] = { 0 };
scanf("%d", &n);
comb(n, 0, 1, num, flag);
system("pause");
return 0;
}
My program is doing fine while 1<=n<=7, but at n=8 and n=9, the FILE pointer becomes NULL in the middle and can only output part of the answers.
I looked closely to the output .txt file and realized that both of the files can only output to the 254th row.I wonder did I make any mistake.
Thanks for answering.
You have hit the limit for the number of open files.
You keep opening the same file for append and don't always close it. When I rearranged the code so that it opens the file once in main in "w" mode, it works correctly.
#include <stdio.h>
#include <stdlib.h>
void comb(FILE *fptr, int n,int curlen, int j, int num[], int flag) {
int i,k;
char ans[20] = { 0 };
if (curlen < n && flag != 0) {
for (i = 0; num[i] != 0; i++) {
ans[i] = num[i] + '`';
}
for (k = 0; k <= curlen; k++) {
fprintf(fptr, "%c", ans[k]);
}
fprintf(fptr, " ");
if (curlen == 0 && num[0] == n) {
//fclose(fptr);
}
else {
comb(fptr, n, curlen + 1, j + 1, num, 0);
}
}
else {
for (; j <= n; j++) {
num[curlen] = j;
comb(fptr, n, curlen, j, num, 1);
}
}
}
int main() {
int n,flag=0,num[20] = { 0 };
char filename[20] = "output-0.txt";
if(scanf("%d", &n) != 1 || n < 1 || n > 9) // sanity check
return 1;
filename[7] = n + '0';
FILE *fptr = fopen(filename, "w"); // open once
if(fptr != NULL) {
comb(fptr, n, 0, 1, num, flag); // pass the file pointer
fclose(fptr);
}
return 0;
}

Why does i never get past 1?

I am trying to create a program to solve a wordsearch from a 2D array using only pointers. In my primary function for doing the actual search for the words I have a while loop that is supposed to keep going as long as i doesn't exceed the length of the word and as long as the letters from the word correspond to the letters on the grid. After finding the word it should make the letters on the grid lowercase and print out that it found the word. Here is my entire program right now.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void printPuzzle(char** arr, int n);
void searchPuzzle(char** arr, int n, char** list, int listSize);
void searchWord(int j, int k, int gridsize, char** arr, char** list, int listPos);
int main(int argc, char **argv) {
int bSize = 15;
if (argc != 2) {
fprintf(stderr, "Usage: %s <puzzle file name>\n", argv[0]);
return 2;
}
int i, j;
FILE *fptr;
char **block = (char**)malloc(bSize * sizeof(char*));
char **words = (char**)malloc(50 * sizeof(char*));
fptr = fopen(argv[1], "r");
if (fptr == NULL) {
printf("Cannot Open Puzzle File!\n");
return 0;
}
for(i=0; i<bSize; i++){
*(block+i) = (char*)malloc(bSize * sizeof(char));
fscanf(fptr, "%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c\n", *(block+i), *(block+i)+1, *(block+i)+2, *(block+i)+3, *(block+i)+4, *(block+i)+5, *(block+i)+6, *(block+i)+7, *(block+i)+8, *(block+i)+9, *(block+i)+10, *(block+i)+11, *(block+i)+12, *(block+i)+13, *(block+i)+14 );
}
fclose(fptr);
fptr = fopen("states.txt", "r");
if (fptr == NULL) {
printf("Cannot Open Words File!\n");
return 0;
}
for(i=0; i<50; i++){
*(words+i) = (char*)malloc(20 * sizeof(char));
fgets(*(words+i), 20, fptr);
}
for(i=0; i<49; i++){
*(*(words+i) + strlen(*(words+i))-2) = '\0';
}
printf("Printing list of words:\n");
for(i=0; i<50; i++){
printf("%s\n", *(words + i));
}
printf("\n");
printf("Printing puzzle before search:\n");
printPuzzle(block, bSize);
printf("\n");
searchPuzzle(block, bSize, words, 50);
printf("\n");
printf("Printing puzzle after search:\n");
printPuzzle(block, bSize);
printf("\n");
return 0;
}
void printPuzzle(char** arr, int n){
for(int i = 0; i < n; i++){
printf("\n");
for(int j = 0; j < 15; j++){
printf("%c ", *(*(arr+i)+j));
}
}
}
void searchPuzzle(char** arr, int n, char** list, int listSize){
for(int i = 0; i < listSize; i++){
for(int j = 0; j < 15; j++){
for(int k = 0; k < 15; k++){
searchWord(j, k, 15, arr, list, i);
}
}
}
}
void searchWord(int j, int k, int gridsize, char** arr, char** list, int listPos){
int wordlength = strlen(*(list+listPos));
if(j+wordlength <= gridsize){ //Horizontal
int i = 0;
while(i < wordlength && *(*(arr+(j+i))+k) == *(*(list+listPos)+i)){
i++;
}
if(i == wordlength){
while(i > 0 && *(*(arr+(j+i))+k) == *(*(list+listPos)+i)){
*(*(arr+(j+i))+k) = tolower(*(*(arr+(j+i))+k));
i--;
}
printf("Word found: ");
for(i = 0; i < wordlength; i++)
printf("%c", *(*(list+listPos)+i));
}
}
if(k+wordlength <= gridsize){ //Vertical
int i = 0;
while(i < wordlength && *(*(arr+j)+(k+i)) == *(*(list+listPos)+i)){
i++;
}
if(i == wordlength){
while(i > 0 && *(*(arr+j)+(k+i)) == *(*(list+listPos)+i)){
*(*(arr+(j+i))+k) = tolower(*(*(arr+(j+i))+k));
i--;
}
printf("Word found: ");
for(i = 0; i < wordlength; i++){
printf("%c", *(*(list+listPos)+i));
}
}
}
if(j+wordlength <= gridsize && k+wordlength <= gridsize){ //Diagonal
int i = 0;
while(i < wordlength && *(*(arr+(j+i))+k) == *(*(list+listPos)+i)){
i++;
}
if(i == wordlength){
while(i > 0 && *(*(arr+(j+i))+(k+i)) == *(*(list+listPos)+i)){
*(*(arr+(j+i))+(k+i)) = tolower(*(*(arr+(j+i))+(k+i)));
i--;
}
printf("Word found: ");
for(i = 0; i < wordlength; i++)
printf("%c", *(*(list+listPos)+i));
}
}
}
This is the part I believe there is a problem with.
while(i < wordlength && *(*(arr+(j+i))+k) == *(*(list+listPos)+i)){
i++;
}
If I print out the decimal value of i after this while loop it should at some point approach the length of one of the words as I know some words are horizontal, however every time it loops through and prints the value of i it never exceeds 1 which means the while loop never runs more than once which is impossible with the wordsearch files I am using to test this. What is causing this to happen? It is also 0 or 1 for every other direction when I try, the example above was just the while loop from the horizontal search.
There may also be a problem with the function searchPuzzle and the amount it loops through. However, I think that may be just my imagination.
Additional Info: The grid is 15x15 and there are 50 words in the list.
I think that you might need to declare Wordsearch again after the && -
while(i < wordlength && wordlength *(*(arr+(j+i))+k) == *(*(list+listPos)+i)){
i++;
}

Writing(in a file) and printing statistics

My task is to create a C program that opens a .c file in which the user then writes some text then said text is printed along with the number of (){}/ and the percentage ratio comments:whole text of the C program.
So far I've this:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int k, j, m, n, l, z, count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0;
char str[10000], chh, chhh;
char ch, file_name[75];
FILE *fp;
printf("Enter the name of file you wish to see with extension .c or .txt\n");
gets_s(file_name);
fp = fopen_s(file_name, "r"); // reads the file
if (fp == NULL)
{
perror("Error while opening the file.\n");
_getche();
exit(EXIT_FAILURE);
}
printf("The contents of %s file are :\n", file_name); //prints out the text
int i = 0;
while ((ch = fgetc(fp)) != EOF) {
printf("%c", ch);
str[i] = ch;
i++;
}
int fsize = i;
// code above opens up the symbols of the file, code below searches for specific symbols
int count = 0;
printf("\nEnter the character to be searched : "); //which symbol to search
scanf_s("%c", &chh);
for (i = 0; i < fsize; i++) {
if (str[i] == chh)
count++;
}
if (count == 0)
printf("\nCharacter '%c' is not present", chh); //if there isn't one
else
printf("\nOccurence of character '%c' : %d", chh, count); //prints their number if there is
for (k = 0; k<fsize; k++) {
if (str[k] == '>')
count1++;
}
for (j = 0; j<fsize; j++) {
if (str[j] == '<')
count2++;
}
for (m = 0; m<fsize - 1; m++) {
if (str[m] == '=' && str[m + 1] == '=')
count3++;
}
for (n = 0; n<fsize - 4; n++) {
if (str[n] == 'e' && str[n + 1] == 'l' && str[n + 2] == 's' && str[n + 3] == 'e')
count4++;
}
for (l = 0; l<fsize - 2; l++) {
if (str[l] == 'i' && str[l + 1] == 'f')
count5++;
}
int br;
br = count4 + count5;
printf("\nOccurence of character '%c' : %d", '>', count1);
printf("\nOccurence of character '%c' : %d", '<', count2);
printf("\nOccurence of character == : %d ", count3);
printf("\nOccurence of character else : %d ", count4);
printf("\nOccurence of character if: %d \n", count5);
printf("\nobsht broi if+else: %d ", br);
fclose(fp);
return 0;
}
It prints out the text inside a file, searches for a specific character you want and prints out its occurrence.
PS: when I try to run it on my PC, Visual Studio spits out a bunch of errors and warnings. I'm puzzled as to how to get rid of them.
Errors image
Thanks !
Using GCC I was able to compile this by changing a few methods.
changed gets_s to gets(file_name) which produces a warning that this is an unsafe function.
changed _getche() to getchar()
changed scanf_s() to scanf()
change fopen_s() to fopen()
This code compiled and ran using GCC on Linux
#include <stdio.h>
#include <stdlib.h>
int main()
{
int k, j, m, n, l, z, count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0;
char str[10000], chh, chhh;
char ch, file_name[75];
FILE *fp;
printf("Enter the name of file you wish to see with extension .c or .txt\n");
gets(file_name);
fp = fopen(file_name, "r"); // reads the file
if (fp == NULL)
{
perror("Error while opening the file.\n");
getchar();
exit(EXIT_FAILURE);
}
printf("The contents of %s file are :\n", file_name); //prints out the text
int i = 0;
while ((ch = fgetc(fp)) != EOF) {
printf("%c", ch);
str[i] = ch;
i++;
}
int fsize = i;
// code above opens up the symbols of the file, code below searches for specific symbols
int count = 0;
printf("\nEnter the character to be searched : "); //which symbol to search
scanf("%c", &chh);
for (i = 0; i < fsize; i++) {
if (str[i] == chh)
count++;
}
if (count == 0)
printf("\nCharacter '%c' is not present", chh); //if there isn't one
else
printf("\nOccurence of character '%c' : %d", chh, count); //prints their number if there is
for (k = 0; k<fsize; k++) {
if (str[k] == '>')
count1++;
}
for (j = 0; j<fsize; j++) {
if (str[j] == '<')
count2++;
}
for (m = 0; m<fsize - 1; m++) {
if (str[m] == '=' && str[m + 1] == '=')
count3++;
}
for (n = 0; n<fsize - 4; n++) {
if (str[n] == 'e' && str[n + 1] == 'l' && str[n + 2] == 's' && str[n + 3] == 'e')
count4++;
}
for (l = 0; l<fsize - 2; l++) {
if (str[l] == 'i' && str[l + 1] == 'f')
count5++;
}
int br;
br = count4 + count5;
printf("\nOccurence of character '%c' : %d", '>', count1);
printf("\nOccurence of character '%c' : %d", '<', count2);
printf("\nOccurence of character == : %d ", count3);
printf("\nOccurence of character else : %d ", count4);
printf("\nOccurence of character if: %d \n", count5);
printf("\nobsht broi if+else: %d \n", br);
fclose(fp);
return 0;
}
Please find my findings below.
gets_s(file_name); -> i dont think this is correct. gets_s Takes up 2 argument and not 1 please use gets instead and try it out or simple scanf to check if it is working.
2._getche() Please use #include header file for that function. Using it would avoid this issue.
fopen_s -> Fopen has invalid set of arguments. U need a file pointer as the first argument. Please re-frame the function used. Better go for fopen which has 2 arguments.
scanf_s -> has an argument(parameter) where you can specify the buffer size. Hence the above code sacnf_s function u have used is syntaticaly wrong. Please change it accordingly.
Please find the changed code which is commonly used. I have taken the liberty of changing the gets_s to gets, fopen_s to fopen and scanf_s to scanf and have build it. No errors. Please find the code below for your reference.
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
FILE *fp;
int main()
{
int k, j, m, n, l, z, count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0;
char str[10000], chh, chhh;
char ch, file_name[75];
printf("Enter the name of file you wish to see with extension .c or .txt\n");
gets(file_name);
fp = fopen(file_name, "r"); // reads the file
if (fp == NULL)
{
perror("Error while opening the file.\n");
getchar();
exit(EXIT_FAILURE);
}
printf("The contents of %s file are :\n", file_name); //prints out the text
int i = 0;
while ((ch = fgetc(fp)) != EOF) {
printf("%c", ch);
str[i] = ch;
i++;
}
int fsize = i;
// code above opens up the symbols of the file, code below searches for specific symbols
int count = 0;
printf("\nEnter the character to be searched : "); //which symbol to search
scanf_s("%c", &chh);
for (i = 0; i < fsize; i++) {
if (str[i] == chh)
count++;
}
if (count == 0)
printf("\nCharacter '%c' is not present", chh); //if there isn't one
else
printf("\nOccurence of character '%c' : %d", chh, count); //prints their number if there is
for (k = 0; k<fsize; k++) {
if (str[k] == '>')
count1++;
}
for (j = 0; j<fsize; j++) {
if (str[j] == '<')
count2++;
}
for (m = 0; m<fsize - 1; m++) {
if (str[m] == '=' && str[m + 1] == '=')
count3++;
}
for (n = 0; n<fsize - 4; n++) {
if (str[n] == 'e' && str[n + 1] == 'l' && str[n + 2] == 's' && str[n + 3] == 'e')
count4++;
}
for (l = 0; l<fsize - 2; l++) {
if (str[l] == 'i' && str[l + 1] == 'f')
count5++;
}
int br;
br = count4 + count5;
printf("\nOccurence of character '%c' : %d", '>', count1);
printf("\nOccurence of character '%c' : %d", '<', count2);
printf("\nOccurence of character == : %d ", count3);
printf("\nOccurence of character else : %d ", count4);
printf("\nOccurence of character if: %d \n", count5);
printf("\nobsht broi if+else: %d ", br);
fclose(fp);
return 0;
}
At any point of time if u need any assistance please ping me :) Thank you.

C: Occurrence of Letters in Text file

Program takes an input file through the command line and outputs the occurrence of each letter in the text file. Not sure where I went wrong.
int main(int argc, char *argv[]) {
char word[1000];
int a = 0;
int b = 0;
int d = 0;
int c = 0;
int e = 0;
int f = 0;
int g = 0;
int h = 0;
int i = 0;
int j = 0;
int k = 0;
int l = 0;
int m = 0;
int n = 0;
int o = 0;
int p = 0;
int q = 0;
int r = 0;
int s = 0;
int t = 0;
int u = 0;
int v = 0;
int w = 0;
int x = 0;
int y = 0;
int z = 0;
int other = 0;
int counter, lenght;
FILE *fp = fopen(argv[1], "r");
fgets(word, 999, fp);
lenght = 1000;
for(counter = 0; counter < lenght; counter++) {
word[counter] = tolower(word[counter]);
if (word[counter] == 'a') {
a++;
}
else if (word[counter] == 'b') {
b++;
}
else if (word[counter] == 'c') {
c++;
}
else if (word[counter] == 'd') {
d++;
}
else if (word[counter] == 'e') {
e++;
}
else if (word[counter] == 'f') {
f++;
}
else if (word[counter] == 'g') {
g++;
}
else if (word[counter] == 'h') {
h++;
}
else if (word[counter] == 'i') {
i++;
}
else if (word[counter] == 'j') {
j++;
}
else if (word[counter] == 'k') {
k++;
}
else if (word[counter] == 'l') {
l++;
}
else if (word[counter] == 'm') {
m++;
}
else if (word[counter] == 'n') {
n++;
}
else if (word[counter] == 'o') {
o++;
}
else if (word[counter] == 'p') {
p++;
}
else if (word[counter] == 'q') {
q++;
}
else if (word[counter] == 'r') {
r++;
}
else if (word[counter] == 's') {
s++;
}
else if (word[counter] == 't') {
t++;
}
else if (word[counter] == 'u') {
u++;
}
else if (word[counter] == 'v') {
v++;
}
else if (word[counter] == 'w') {
w++;
}
else if (word[counter] == 'x') {
x++;
}
else if (word[counter] == 'y') {
y++;
}
else if (word[counter] == 'z') {
z++;
}
else {
other++;
}
}
printf("\nCharacter frequency in %s", argv[1]);
printf("\nCharacter Count");
printf("\na \t\t %d", a);
printf("\nb \t\t %d", b);
printf("\nc \t\t %d", c);
printf("\nd \t\t %d", d);
printf("\ne \t\t %d", e);
printf("\nf \t\t %d", f);
printf("\ng \t\t %d", g);
printf("\nh \t\t %d", h);
printf("\ni \t\t %d", i);
printf("\nj \t\t %d", j);
printf("\nk \t\t %d", k);
printf("\nl \t\t %d", l);
printf("\nm \t\t %d", m);
printf("\nn \t\t %d", n);
printf("\no \t\t %d", o);
printf("\np \t\t %d", p);
printf("\nq \t\t %d", q);
printf("\nr \t\t %d", r);
printf("\ns \t\t %d", s);
printf("\nt \t\t %d", t);
printf("\nu \t\t %d", u);
printf("\nv \t\t %d", v);
printf("\nw \t\t %d", w);
printf("\nx \t\t %d", x);
printf("\ny \t\t %d", y);
printf("\nz \t\t %d", z);
fclose(fp);
return 0;
}
Should output in two columns one being the letter and the next being the number of times that letter occurs
There are problems in your code:
you do not include <stdio.h> nor <ctype.h>
you only read one line and you do not even check if that succeeds. You should write a loop like while (fgets(word, sizeof word, fp)) {
you check all characters in the word array: you should stop at the end of the line: lenght = strlen(word);
tolower() should not be given a char argument, because on platforms where char is signed, negative values invoke undefined behavior. You can cast the argument as (unsigned char) to avoid this: word[counter] = tolower((unsigned char)word[counter]);
More room for improvement:
lenght is misspelt, it should be length.
you should use an array of counters to avoid all these tests and all these explicit printf statements.
check the argument count and fopen() success
no need to read line by line, handle one byte at a time read with getc(). However, reading one large chunk at a time can be faster because it uses fewer tests and locks.
the printf statements should output the newline at the end rather than at the beginning.
Here is a corrected and simplified version:
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
int count[UCHAR_MAX + 1] = { 0 };
int other, total;
int c;
const char *s;
FILE *fp;
if (argc <= 1) {
fprintf(stderr, "missing input file\n");
return 1;
}
fp = fopen(argv[1], "r");
if (fp == NULL) {
fprintf(stderr, "cannot open input file %s\n", argv[1]);
return 1;
}
total = 0;
while ((c = getc(fp)) != EOF) {
count[tolower(c)] += 1;
total++;
}
printf("Character frequency in %s\n", argv[1]);
printf("Character Count\n");
other = total;
for (s = "abcdefghijklmnopqrstuvwxyz"; *s; s++) {
printf("%c:\t%9d\n", *s, count[(unsigned char)*s]);
other -= count[(unsigned char)*s];
}
printf("other:\t%9d\n", other);
fclose(fp);
return 0;
}
Reading the file by chunks instead of one byte at a time improves the speed dramatically with recent C libraries, because the support for multithreading has made the getc() macros inefficient. Using 64K buffers, the code below is fifty times faster (50X) for a 400MB file:
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#define BUFFER_SIZE 65536
int main(int argc, char *argv[]) {
unsigned char buffer[BUFFER_SIZE];
long count[UCHAR_MAX + 1] = { 0 };
long other;
size_t i, n;
const char *s;
FILE *fp;
if (argc <= 1) {
fprintf(stderr, "missing input file\n");
return 1;
}
fp = fopen(argv[1], "r");
if (fp == NULL) {
fprintf(stderr, "cannot open input file %s\n", argv[1]);
return 1;
}
while ((n = fread(buffer, 1, sizeof buffer, fp)) != 0) {
for (i = 0; i < n; i++) {
count[buffer[i]] += 1;
}
}
other = 0;
for (i = 0; i <= UCHAR_MAX; i++) {
if (isupper(i)) {
count[tolower(i)] += count[i];
} else {
if (!islower(i))
other += count[i];
}
}
printf("Character frequency in %s\n", argv[1]);
printf("Character Count\n");
for (s = "abcdefghijklmnopqrstuvwxyz"; *s; s++) {
printf("%c:\t%9ld\n", *s, count[(unsigned char)*s]);
}
printf("other:\t%9ld\n", other);
fclose(fp);
return 0;
}
Here's a quick implementation I wrote. It doesn't use fgets, but that is most definitely an option.
The flow of the program should be simple, but it is as follows:
Check for a proper argument count.
Declare the variables we'll need.
Declare the file pointer and attempt to open the file.
If the file doesn't open, we'll error out.
Read in every character from the file one at a time and store it into our variable c.
Using our ascii table, we'll alter the values to get them into the proper position in our array.
Print out all of our values.
Close the file.
#include <stdio.h>
int main(int argc, char **argv){
if (argc < 2){
printf("Not enough arguments!\n");
return -1;
}
int A[27] = {0}, c;
FILE *inFile = fopen(argv[1], "r");
if (inFile == NULL){
printf("The file \"%s\" could not be opened.\n", argv[1]);
return -2;
}
while((c = fgetc(inFile)) != EOF){
if ( c >= 'a' && c <= 'z' ){
/* C is a lowercase character */
c-='a';
A[c]++;
}
else if ( c >= 'A' && c <= 'Z' ){
/* C is an uppercase character */
c-='A';
A[c]++;
}
else if (c == '\n'){
/* we're not counting newlines */
continue;
}
else {
A[26]++;
}
}
/* Print out all the values except the "Other" count. */
for (c = 0; c < sizeof A / sizeof A[0] - 1; c++){
printf("%c: %d\n", c+'a', A[c]);
} printf("Other: %d\n", A[26]); //Print out "Other" count
/* Close our file */
fclose(inFile);
return 0;
}

Storing numbers as (x, y) cordinates from a file at a specific point

I have an Instance File from which I need to store the NUM_PT and all the respective co-ordinates in the form of a 2D array system (personal choice so I can access them easily). I am able to retrieve the NUM_PT but I am stuck at reading the successive cordinates into my array.
HERE IS WHAT I HAVE DONE
/* Assignment 2 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#define MAXS 256
int main(int argc, char *argv[])
{
int num_pt;
int inputfile = 0, outputfile = 0, i;
for (i = 1; i < argc; i++)
{
if (strcmp (argv[i], "-i") == 0)
inputfile = i+1;
if (strcmp (argv[i], "-o") == 0)
outputfile = i+1;
}
if (inputfile == 0)
{
/* invalid command line options */
printf("\nIncorrect command-line...\n");
printf("> %s [-i inputfile [-o outputfile]]\n\n", argv[0]);
exit(0);
}
FILE *fp;
fp = fopen(argv[inputfile], "r");
int count = 0;
if (fp == 0)
{
printf("\nCould not find %s\n", argv[inputfile]);
exit(0);
}
char line[MAXS];
while (fgets(line, sizeof line, fp) != NULL)
{
if (count == 4)
{
fscanf(fp, "%d", &num_pt);
break;
}
else
count++;
}
int arr[num_pt][1];
while (fgets(line, sizeof line, fp) != NULL)
{
if (count == 5)
{
int k, j, cord;
for (k = 0; k < num_pt; k++)
{
for (j = 0; j < num_pt; j++)
{
while (fscanf(fp, "%d%d", &cord) > 0)
{
arr[k][j] = cord;
j++;
}
}
}
}
}
fclose(fp)
return 0;
}
After retrieving NUM_PT i tried reinitializing the count to 5 because the cordinates start from **LINE 6* in the file.
ERROR FROM COMPILER
Language: c99 ; Compiler: gcc
sample for "Storing numbers as (x, y) cordinates from a file" (It is better not to fix the reading position)
#include <stdio.h>
typedef struct point {
int x, y;
} Point;
int readPoint(FILE *fp, Point *p);
int readInt(FILE *fp, int *n);
int main(void){
FILE *fp = fopen("instance10_001.txt", "r");
Point p;
int MAX_X, MAX_Y;
readPoint(fp, &p);
MAX_X = p.x;
MAX_Y = p.y;
printf("MAX_X:%d, MAX_Y:%d\n", MAX_X, MAX_Y);
int NUM_PT;
readInt(fp, &NUM_PT);
printf("NUM_PT:%d\n", NUM_PT);
Point arr[NUM_PT];
for(int i = 0; i < NUM_PT; ++i){
readPoint(fp, &arr[i]);
printf("Point(%d, %d)\n", arr[i].x, arr[i].y);
}
fclose(fp);
}
int readLine(FILE *fp, char *buff, int buff_size){
while(fgets(buff, buff_size, fp)){
if(*buff == '#' || *buff == '\n')
continue;
return 1;
}
return 0;
}
#define LINE_MAX 128
int readPoint(FILE *fp, Point *p){
char buff[LINE_MAX];
if(readLine(fp, buff, sizeof buff)){
return 2 == sscanf(buff, "%d %d", &p->x, &p->y);
}
return 0;
}
int readInt(FILE *fp, int *n){
char buff[LINE_MAX];
if(readLine(fp, buff, sizeof buff)){
return 1 == sscanf(buff, "%d", n);
}
return 0;
}

Resources