rrz0
rrz0

Reputation: 2302

File Handling and Functions in C

I have come across the following problem.

I have a program which allows the user to create a .txt file and add up to 10 ASCII values. I am then closing the file, and re-opening in read mode. The point of this being that I am converting the inputted ASCII to integer using ATOI.

The working code for this is provided below.

My problem is: I want to create some sort of array which stores these inputted ASCII values. BY doing so, this will enable me to call upon a function to check which of these ASCII values is smallest.

fp = fopen("c:\\CTEMP\\1.txt", "w+");
{
    for (x = 0; x < 10; x++)
    {
        printf("\nType the word you want to add. Type exit to terminate: ");
        scanf("%s", word); // word is declared as char word[10]

        if (strcmp(word, "exit") == 0)
        {
            break;
        }
        fprintf(fp, "%s\n", word);
        words++;
    }
    fclose(fp);
}
fp = fopen("C:\\CTEMP\\1.txt", "r");

while (!feof(fp))
{
    fscanf(fp, "%s", &word);
    number = atoi(word);
    printf("\nstring  is \t %s\n", word);
    printf("integer is \t %d\n", number);

    //  location = find_minimum(array,number);
    //  minimum = array[location];

    //  printf("Minimum element location = %d and value = %d.\n", location + 1, minimum);       
}
scanf_s("%d");
}
  1. Am I tackling the problem of finding the smallest ASCII value correctly?
  2. Is there any other way without creating another array to store the ASCII values?

Upvotes: 0

Views: 123

Answers (2)

Stephan Lechner
Stephan Lechner

Reputation: 35154

As mentioned by Barmar, there is no need to store all the values in an array just for the sake of finding the minimum then. Let a variable minNr store the smallest number read so far, and let minIdx store it's index. Whenever the number read in the current pass is smaller (or equal) then minNr, adapt minNr and minIdx accordingly. Thereby, for any two equal numbers read in, the latter will be considered as the minimum's index. Note that minNr is initialized with INT_MAX, such that already the very first number read in will "beat" this initial value:

int finished = 0;
int minNr = INT_MAX;
int minIdx = 0;

fp = fopen("C:\\CTEMP\\1.txt", "r");
if (fp==NULL)
  finished=1;

for (int i=1; !finished; i++)
{
    char word[50];
    if (fscanf(fp, "%s", word) < 1)
        finished = 1;
    else {
        int number = atoi(word);
        printf("\nstring  is \t %s\n", word);
        printf("integer is \t %d\n", number);

        if (number <= minNr) {
            minNr = number;
            minIdx = i;
        }
    }
}
if (minIdx > 0)
    printf ("min number is %d at position %d\n", minNr, minIdx);
else
    printf("no numbers read in; hence: no minimum calculated.");

BTW: in your code, if word is declared as something like char word[50], then statement fscanf(fp, "%s", &word) should give you at least a compiler warning because of the superfluous &.

Upvotes: 1

Petr Skocik
Petr Skocik

Reputation: 60068

My problem is: I want to create some sort of array which stores these inputted ASCII values. BY doing so, this will enable me to call upon a function to check which of these ASCII values is smallest.

I don't know what an ASCII value is but here's how you:

 char chars[10]; //create an array of 10 characters
 int ints[10]; //create an array of 10 ints
 char strings[10][10]; //create an array of 10 9-character strings 

Am I tackling the problem of finding the smallest ASCII value correctly?

Semantically, perhaps. But you're 1) being spacially inefficient by trying to store all the elements (you probably don't need to store them -- not in memory, not on disk) and 2) you're failing to do error handling on the IO functions you call. Additionally 3) you're creating a security hole with scanf("%s", without a length limit.

Is there any other way without creating another array to store the ASCII values?

Yes. Store only your current candidate for the minimum value. The rest can be forgotten.

Upvotes: 1

Related Questions