Okay, so I figured out that I was using an array and that's not what I want. So I changed my code based on some examples I found in my workbook and on StackOverflow, but it still does not work!?! Here is my current code (very similar), and what happens when I run it is it takes the read/write files correctly, but when the While loop runs, it just displays "There was no data to read from!" Clearly, the sscanf is outputting an integer when my switch requires a character. I just don't understand how to do this!?! It seems like it should be so simple, but I guess I'm C retarded at this point because I've read easily over fifty full examples at this point, and I still don't comprehend what I'm doing incorrectly . Here is my current code if anyone has any suggestions...
Code:
/*
============================================================================
Name : xxxxx.c
Author : xxxxx
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* required to run strcat, which adds text to the end of a string */
#include <dirent.h> /* library for showing all files in a directory */
int main(void) {
setvbuf(stdout, NULL, _IONBF, 0);
unsigned int a = 0, c = 0, g = 0, t = 0;
int stringCH;
char filename[BUFSIZ], savename[BUFSIZ], lineCH[BUFSIZ+1];
FILE *pInFile, *pOutFile;
/* LISTS ALL FILES IN DIRECTORY */
DIR *d;
struct dirent *dir;
d = opendir(".");
if (d) {
printf("Here are the files in this directory:\n");
while ((dir = readdir(d)) != NULL) {
printf("%s\n", dir->d_name);
}
closedir(d);
}
/* INPUT READ FILE FROM USER */
printf(
"\nPlease type the name of a TEXT file to read from, without the extension:\n");
fgets(filename, BUFSIZ, stdin); /* user inputs file to load */
strtok(filename, "\n"); /* removes the \n from fgets */
strcat(filename, ".txt"); /* adds .txt to the string */
if ((pInFile = fopen(filename, "r")) == 0) {
printf(
"You entered %s which is invalid. You must enter a proper TEXT file! Goodbye!\n",
filename);
fclose(pInFile);
exit(0);
}
/* INPUT WRITE FILE FROM USER */
printf(
"Excellent! Now create a name of a TEXT file to write to, without an extension:\n");
fgets(savename, BUFSIZ, stdin); /* user inputs file to load */
strtok(savename, "\n"); /* removes the \n from fgets */
strcat(savename, ".txt"); /* adds .txt to the string */
pOutFile = fopen(savename, "w");
/* EXECUTION OF DATA RETREVIAL */
printf("Great! Retrieving data from the READ file:\n\n");
/////////////////////PROBLEM IS HERE SOMEWHERE...////////////////////////
while (fgets(lineCH, BUFSIZ, pInFile) != NULL) {
sscanf(lineCH,"%1d",&stringCH);
switch (stringCH) {
/////////////////////PROBLEM IS HERE SOMEWHERE...////////////////////////
case 'a':
++a;
break;
case 'c':
++c;
break;
case 'g':
++g;
break;
case 't':
++t;
break;
default:
printf("There was no data to read from!\n");
break;
} //--> END OF SWITCH
} //--> END OF WHILE LOOP
if (feof(pInFile)) //--> AFTER END OF WHILE LOOP/CHECKS FOR END-OF-FILE TRUE
{
printf("Results are as follows:\n");
printf("A\tC\tG\tT\n");
printf("%d\t%d\t%d\t%d\t\n", a, c, g, t);
fprintf(pOutFile, "%d %d %d %d", a, c, g, t);
fclose(pInFile);
fclose(pOutFile);
fprintf(stdout,"Complete\n");
} else if (ferror(pInFile)) //--> IF ERROR READING FILE, GOODBYE!
{
printf("Error reading from file. Goodbye!\n");
fclose(pInFile);
fclose(pOutFile);
exit(0);
}
return EXIT_SUCCESS;
} //--> END OF MAIN