PDA

View Full Version : C, bringing in numbers from text file



Ejjman
05-02-2012, 06:02 PM
I have two text files consisting of integers in ascending order, with no duplicates. This will be provided in the form of

-5
0
4
2

I have to take each .txt file, and create a new .txt file that has a combination of these numbers with no duplicates, also in ascending order.

I was thinking something like this:

Take one .txt file, copy it into the new .txt file that is being created. Then, take the other .txt file, and go line by line and look for duplicates or values that are smaller, and place them in that correct spot within the new .txt file that was created earlier.

The problem is, I've never worked with using .txt files and manipulating of this sort. I also am not allowed to use arrays.

Any suggestions will be taken, I don't really know where to begin.

Echo_
05-02-2012, 07:49 PM
1. Sort the file first, reading two values at a time and if the first is larger than the second, switch them when writing to the result file. Continue reading through the original file until you have read the whole thing. After that, switch to your result file and continue sorting. Continue doing this until you make a successful pass through the file without needing to sort at all (use a bool as a flag).

int left;
int right;
fscanf(fp, "%d %d", &left, &right);

2. After the file is sorted, use the same algorithm looking at 2 values at a time to see if there are duplicates. If the two values loaded are the same, omit one from the next file write.

I would post some code, but I'm at a school computer without a C implementation. I'll post examples later if I have time.

Ejjman
05-03-2012, 04:36 PM
Both text files are already in ascending order that I receive.

I kinda see what you are saying, how do I manipulate that result file? Like, if there's a number that is smaller, how do I place it between two already placed numbers? Also, how do I omit one?

Thx for the post.

MrJewbagel
05-21-2012, 11:19 PM
Both text files are already in ascending order that I receive.

I kinda see what you are saying, how do I manipulate that result file? Like, if there's a number that is smaller, how do I place it between two already placed numbers? Also, how do I omit one?

Thx for the post.

Not sure if this was ever solved, but I have an old source file that may be of help. It shows how to read and write to a file. If you have any further questions ask. (I'm not extremely fluent in C, but I know some.) :P

EDIT: WOW, I don't know how I missed the "not able to use arrays" part. If that's the case, you will just need to use several different variables and several loops, as the for loop I use needs the array.

Example
#include <stdio.h>

int fill_array(void);
int display_array(int count);

typedef struct
{
char worker_name[20];
float hourly_pay;
int hours;
}WorkerRecord;

WorkerRecord info[50];

int main()
{
int count, k;
float average_wage, total_wage = 0, total_hourly = 0;
float gross_pay[50];
FILE * ptr;

count = fill_array();
display_array(count);

for(k = 0; k < count; k++)
{
total_hourly += info[k].hourly_pay;
}

average_wage = total_hourly /count;

printf("\nAverage Hourly Wage: $%.2f\n\n", average_wage);

for(k = 0; k < count; k++)
{
gross_pay[k] = (info[k].hourly_pay * info[k].hours);
total_wage += gross_pay[k];
}

printf("This Week's Total Payroll: $%.2f", total_wage);

ptr = fopen("report.txt", "w");

fprintf(ptr, " Name\t\tHours\t\tHourly Pay\tGross Pay\n");
fprintf(ptr, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~\n");

for(k = 0; k < count; k++)
{
fprintf(ptr, "%s\t\t%d\t\t$%.2f\t\t$%.2f\n", info[k].worker_name, info[k].hours, info[k].hourly_pay, gross_pay[k]);
}

fprintf(ptr, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~\n");
fprintf(ptr, "\nAverage Hourly Wage: $%.2f\n\n", average_wage);
fprintf(ptr, "This Week's Total Payroll: $%.2f", total_wage);

fclose(ptr);

printf("\n\nPress any key to continue...");
while(!getchar());
return 0;
}

int fill_array(void)
{
int count = 0;

FILE * pointer;

pointer = fopen("workers.txt", "r");

if(pointer == NULL)
{
printf("The file can not be found.\n\n\n");
printf("Press any key to continue...");
getchar();
return 1;
}

while(!feof(pointer))
{
fscanf(pointer, "%s%d%f", info[count].worker_name, &info[count].hours, &info[count].hourly_pay);
count++;
}

fclose(pointer);

return (count);
}

int display_array(int count)
{
int k;
float gross_pay[50];

printf(" Name\t\tHours\t\tHourly Pay\tGross Pay\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~\n");

for(k = 0; k < count; k++)
{
gross_pay[k] = (info[k].hourly_pay * info[k].hours);
}

for(k = 0; k < count; k++)
{
printf("%s\t\t%d\t\t$%.2f\t\t$%.2f\n", info[k].worker_name, info[k].hours, info[k].hourly_pay, gross_pay[k]);
}

printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~\n");

return 0;
}

Yes I know you didn't need all of it, but just in case you wanted to test it.

Input File

Bob 40 10
Sean 35 12
Linda 30 15
Michael 45 9
Kelly 25 11
(Wow, code tags don't like tabs or using 5 spaces.)

MrJewbagel
05-21-2012, 11:27 PM
Also, as far as reordering variables in ascending/descending order, as far as I know that still involves writing your own function.

Edit: Also, not sure if you know, but if you wanted to do more than just create a new file.


ptr = fopen("report.txt", "w");

r - open for reading
w - open for writing (file need not exist)
a - open for appending (file need not exist)
r+ - open for reading and writing, start at beginning
w+ - open for reading and writing (overwrite file)
a+ - open for reading and writing (append if file exists)