Reading from a File to an Array - C - c

int main()
{
FILE* infile1;
int stockCode[15];
char stockName[100];
int stockQuantity[15];
int stockReorder[15];
int unitPrice[15];
int i;
infile1 = fopen("NUSTOCK.TXT", "r");
while(fscanf(infile1, "%d %s %d %d %f",
&stockCode, stockName, &stockQuantity, &stockReorder, &unitPrice) != EOF)
{
printf(" %3d %-18s %3d %3d %6.2f \n",
stockCode, stockName, stockQuantity, stockReorder, unitPrice);
}
fclose(infile1);
}
What I'm trying to do is to take information from a file and store it into 5 separate arrays. However, when printing out, it only prints out the name correctly.
1394854864 Prune-Basket 1394854688 1394854624 0.00
1394854864 Pear-Basket 1394854688 1394854624 0.00
1394854864 Peach-Basket 1394854688 1394854624 0.00
1394854864 Deluxe-Tower 1394854688 1394854624 0.00
The original file looks like this. So all the numbers aren't being scanned in and I can't figure out why...
101 Prune-Basket 065 060 25.00
105 Pear-Basket 048 060 30.00
107 Peach-Basket 071 060 32.00
202 Deluxe-Tower 054 040 45.00

I think what you want to do is designing a structure for saving many personal records.
And each record contain:
code
name
quantity
reorder
unitPrice
You should know the meaning of each type in C language.
I suggest you rewrite your codes like these:
#include <stdio.h>
#include <stdlib.h>
struct OneRecord{
int code;
char name[100];
int quantity;
int recorder;
float unitPrice;
};
int main(){
struct OneRecord* records = (struct OneRecord*)calloc(15, sizeof(struct OneRecord));
int i = 0;
FILE* infile1 = fopen("NUSTOCK.TXT", "r");
int max=0;
//%99s is used for max string length, because of we can protect the out of string's memory length
while((max<15)&&(fscanf(infile1, "%d %99s %d %d %f",
&records[i].code, records[i].name, &records[i].quantity, &records[i].recorder, &records[i].unitPrice) == 5))
{
i++;
max++;
}
for(i=0;i<max;i++){
printf(" %3d %-18s %3d %3d %6.2f \n",
records[i].code,records[i].name,records[i].quantity,records[i].recorder,records[i].unitPrice);
}
fclose(infile1);
free(records);
}
And how to use the C Structure in functions or many other places?
In C programming language, there are different types such as int, char, struct and so on. You can use struct like many other types.
void printRecords(const struct OneRecord* records, int max)
{
int i;
for(i=0;i<max;i++){
printf(" %3d %-18s %3d %3d %6.2f \n",
records[i].code,records[i].name,records[i].quantity,records[i].recorder,records[i].unitPrice);
}
}

You are using those arrays incorrectly. Try this:
i = 0;
while(fscanf(infile1, "%d %s %d %d %f",
stockCode+i, stockName, stockQuantity+i, stockReorder+i, unitPrice+i) != EOF)
{
printf(" %3d %-18s %3d %3d %6.2f \n",
stockCode[i], stockName, stockQuantity[i], stockReorder[i], unitPrice[i]);
i++;
}
Also, the unitPrice should be a array of float, not of int.

Related

Trouble getting sscanf to work correctly with certain inputs

I'm trying to read the values in those lines to the variables with sscanf and I'm getting very weird results. It works with some lines as long as I use floats, but with other similar lines with floats it doesn't work, and if I use doubles instead of floats it never works properly.
#include <stdio.h>
#include <stdlib.h>
void debug(char* line,int* year, int* month, int* day, double* temp, double* uncertainty,
char country[100]){
int result;
result = sscanf(line, "%i - %i - %i, %lf , %lf , %s", year,
month, day, temp, uncertainty, country);
printf("%i-%i-%i,%f,%f,%s\n",*year, *month, *day, *temp,
*uncertainty, country);
printf("Result:%i\n", result);
}
void debug_f(char* line, int* year, int* month, int* day, float* temp, float* uncertainty,
char country[100]){
int result;
result = sscanf(line, "%i - %i - %i, %f , %f , %s", year,
month, day, temp, uncertainty, country);
printf("%i-%i-%i,%lf,%lf,%s\n",*year, *month, *day, *temp,
*uncertainty, country);
printf("Result:%i\n", result);
}
int main(){
char* error = "1943-09-01,29.27,0.403,Yemen";
char* working = "1972-03-01,4.787,0.342,Slovakia";
int year1, year2, year3, year4;
int month1, month2, month3, month4;
int day1, day2, day3, day4;
double temp1, temp2;
double uncertainty1, uncertainty2;
float temp3, temp4;
float uncertainty3, uncertainty4;
char country1[100], country2[100], country3[100], country4[100];
debug(error, &year1, &month1, &day1, &temp1, &uncertainty1, country1);
debug(working, &year2, &month2, &day2, &temp2, &uncertainty2, country2);
debug_f(error, &year3, &month3, &day3, &temp3, &uncertainty3, country3);
debug_f(working, &year4, &month4, &day4, &temp4, &uncertainty4, country4);
}
This is the output I get on my machine:
1943-0-0,0.000000,0.000000,�\��
Result:2
1972-3-1,0.000000,0.000000,Slovakia
Result:6
1943-0-0,0.000000,0.000000,
Result:2
1972-3-1,4.787000,0.342000,Slovakia
Result:6
Try removing the space used in between date elements in sscanf.
sscanf(line, "%i-%i-%i, %f , %f , %s",
&int1, &int2,
&int3, &double1, &double2,
s);
As EugeneSh pointed above its signed integer, and reading float to a double.
"%i" differs from "%d" when scanning
"1943-09-01,29.27,0.403,Yemen"; fails because "09" is not scanned into an int as 9 with "%i".
In the below code, when scanning for month, sscanf(), using "%i", encounters "09". Since it leads with a'0', that switches interpretation to octal. As '9' is not an octal digit, month is assigned 0 and scanning continues with " - %i, %lf , %lf , %s". Since '9' does not match '-', scanning stops and reports 2 successful conversions.
sscanf(line, "%i - %i - %i, %lf , %lf , %s", year, month, day, temp, uncertainty, country);
Suggested alternative
Use "%d" instead of "%i" to insure decimal interpretation.
Use width limits when saving a string.
Use "%n" to detect end of scan and look for extra junk.
void debug(const char* line, int* year, int* month, int* day,
double* temp, double* uncertainty, char country[100]){
int n = 0;
int result = sscanf(line, "%d - %d - %d , %lf , %lf , %99s %n",
year, month, day, temp, uncertainty, country, &n);
printf("Result:%i\n", result);
if (n > 0 && line[n] == '\0') {
printf("%i-%i-%i,%f,%f,%s\n",
*year, *month, *day, *temp, *uncertainty, country);
} else {
printf("<%s> failed\n", line);
}
}
Note that %s will not fully read country names like "Sri Lanka", but only the first "word" "Sri". Depending on coding goals, consider:
int result = sscanf(line, "%d - %d - %d , %lf , %lf , %99[^\n] %n",

value in loop doesnt return

i have this data in file
ATOM 1 N MET A 1 25.909 16.164 -8.037
ATOM 2 CA MET A 1 25.498 14.786 -8.206
ATOM 3 C MET A 1 26.612 13.934 -8.806
ATOM 4 O MET A 1 27.442 14.402 -9.588
this is my full code
#include<stdio.h>
#include<math.h>
typedef struct prot {
char atom[10];
int atomno;
char site[10];
char residue[10];
char chain;
int residueno;
float x, y, z;
} Td;
int main()
{
FILE*fd;
fd=fopen("2zoi.pdb","r+");
Td data[1300];
char buffer[1300];
int a=0;
while(fgets(buffer, 1300, fd))
{
sscanf(buffer, "%s %d %s %s %c %d %f %f %f",data[a].atom,&data[a].atomno,data[a].site,
data[a].residue,&data[a].chain,&data[a].residueno,&data[a].x, &data[a].y, &data[a].z);
printf("%s %d %s %s %c %d %6.3f %6.3f %6.3f \n",data[a].atom,data[a].atomno,data[a].site,
data[a].residue,data[a].chain,data[a].residueno,data[a].x, data[a].y, data[a].z);
a++;
}
//---user input
int fr,sr;
int fa,sa;
int i=0;
float x1,x2,y1,y2,z1,z2;
float distance=0;
printf("Enter first no of atom :");
scanf("%d",&fa);
printf("Enter second no of atom :");
scanf("%d",&sa);
while(data[i].atomno>=0)
{
if(fa==data[i].atomno)
{
x1=data[i].x;
y1=data[i].y;
z1=data[i].z;
}
else if(sa==data[i].atomno)
{
x2=data[i].x;
y2=data[i].y;
z2=data[i].z;
}
i++;
}
printf("%f %f %f",x1,y1,z1);
printf("%f %f %f",x2,y2,z2);
//distance= sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2) + pow((z2 - z1), 2));
//printf("%6.3f",distance);
return 0;
}
the problem is at this part
printf("%f %f %f",x1,y1,z1);
printf("%f %f %f",x2,y2,z2);
i try to return to values from above loop where the value for x1,y1,z1 is for the first atom and x2,y2,z2 for second atom.
when i input 3 and 4, it gives answer
26.612 13.934 -8.806
27.442 14.402 -9.588
which is correct. but when i input 1 and 2, it gives rubbish answer. it seems like i cant input number 2 also number 10.did i do any mistake?
You have a big problem with data array values: not all elements of the array are set to 0 then the final loop can be broken.
You can use memset to reset to 0 all bytes of data array.
#include<stdio.h>
#include<math.h>
#include <string.h>
typedef struct prot
{
char atom[10];
int atomno;
char site[10];
char residue[10];
char chain;
int residueno;
float x, y, z;
} Td;
int main()
{
FILE*fd;
Td data[1300];
char buffer[1300];
size_t a=0;
fd=fopen("2zoi.pdb","r+");
if (fd != NULL)
{
memset(data, 0x00, sizeof(data));
while ((fgets(buffer, 1300, fd)) && (a < sizeof(data)/sizeof(data[0])))
{
sscanf(buffer, "%s %d %s %s %c %d %f %f %f",data[a].atom,&data[a].atomno,data[a].site,
data[a].residue,&data[a].chain,&data[a].residueno,&data[a].x, &data[a].y, &data[a].z);
printf("%s %d %s %s %c %d %6.3f %6.3f %6.3f \n",data[a].atom,data[a].atomno,data[a].site,
data[a].residue,data[a].chain,data[a].residueno,data[a].x, data[a].y, data[a].z);
a++;
}
//---user input
int fr,sr;
int fa,sa;
size_t i=0;
float x1,x2,y1,y2,z1,z2;
float distance=0;
printf("Enter first no of atom :");
scanf("%d",&fa);
printf("Enter second no of atom :");
scanf("%d",&sa);
while ((data[i].atomno>=0) && (i < sizeof(data)/sizeof(data[0])))
{
if(fa==data[i].atomno)
{
x1=data[i].x;
y1=data[i].y;
z1=data[i].z;
}
else if(sa==data[i].atomno)
{
x2=data[i].x;
y2=data[i].y;
z2=data[i].z;
}
i++;
}
printf("%f %f %f\n",x1,y1,z1);
printf("%f %f %f\n",x2,y2,z2);
}
else
{
fprintf(stderr, "Error opening file\n");
exit(1);
}
return 0;
}
You should check the fopen return value (as you can see in code posted), but as a standard rule: you should always check functions return values.

Read variables from a file and store them in structs - C

I'm having some serious trouble with this program.
Pretty much I need to read from a file that has a set of first and last name, a cookie type, the amount purchased, the cost, and whether is was paid for or not.
I'm fairly confident that my functions are correct.
When I run the program, it runs correctly but there looks to be extra digits in between certain numbers. What I mean is, if the input file is:
John Green mint 1 1.00 Y
Jane Blue chip 2 2.00 N
The output file will be something like:
John Green 12 20000 mint 455 1 etc...
Any ideas? I'm guessing I messed up in reading the file/storing them into the struct.
#include <stdio.h>
#include <ctype.h>
/*structure definition*/
struct customer{
char first[12], last[12], type[10], paid;
int boxes;
float cost;
};
/*prototypes*/
void readCustomerData(FILE*, struct customer[]);
void printCustomerData(FILE*, int, struct customer[]);
float moneyReceived(int, struct customer[]);
int custWhoOwe(int, struct customer[]);
float moneyOutstanding(int, struct customer[]);
int moneyReceivedPercent(int, struct customer[]);
int main (){
int numCustomers;
float money, money2;
int cust, perc;
FILE*input;
FILE*output;
input = fopen("custInfo.txt", "r");
output = fopen("output.txt", "w");
fscanf(input, "%d", &numCustomers);
struct customer customerData[numCustomers];
readCustomerData(input, customerData);
printCustomerData(output, numCustomers, customerData);
fprintf(output, "Summary:\n\n");
money = moneyReceived(numCustomers, customerData);
fprintf(output, "Total Cash Received: $%.2f\n\n", money);
cust = custWhoOwe(numCustomers, customerData);
fprintf(output, "Num customers who have not paid: %d\n\n", cust);
money2 = moneyOutstanding(numCustomers, customerData);
fprintf(output, "Total Amount Outstanding: $%.2f\n\n", money2);
perc = moneyReceivedPercent(numCustomers, customerData);
fprintf(output, "Percentage of Sales outstanding: %d%%\n\n", perc);
fclose(input);
fclose(output);
return 0;
}
void readCustomerData(FILE*file, struct customer cdata[]){
int numCustomers, i;
file = fopen("custInfo.txt", "r");
fscanf(file, "%d", &numCustomers);
for (i=0; i<numCustomers; i++){
fscanf(file, "%s%s%s%d%f%c", &cdata[i].first, &cdata[i].last, &cdata[i].type, &cdata[i].boxes, &cdata[i].cost, &cdata[i].paid);
}
fclose(file);
}
void printCustomerData(FILE*file, int num, struct customer cdata[]){
int i;
fprintf(file, "Customers:\n\n");
fprintf(file, "Name\t\tType of Cookie\t\tNum of Boxes\tCost of Box\tPaid\n");
fprintf(file, "--------------------------------------------------------------------------------\n\n");
for (i=0; i<num; i++){
fprintf(file, "%s %s\t", cdata[i].first, cdata[i].last);
fprintf(file, "%s\t\t", cdata[i].type);
fprintf(file, "%d\t\t", cdata[i].boxes);
fprintf(file, "$%.2f\t\t", cdata[i].cost);
fprintf(file, "%c", cdata[i].paid);
fprintf(file, "\n\n");
}
fprintf(file, "\n\n\n\n\n");
}
float moneyReceived(int num, struct customer cdata[]){
int i, sum=0;
for (i=0; i<num; i++){
if(cdata[i].paid=='Y'){
sum= sum+(cdata[i].cost*cdata[i].boxes);}
}
return sum;
}
int custWhoOwe(int num, struct customer cdata[]){
int count=0, i;
for (i=0; i<num; i++){
if (cdata[i].paid=='N'){
count++;}
}
return count;
}
float moneyOutstanding(int num, struct customer cdata[]){
int i, sum=0;
for (i=0; i<num; i++){
if (cdata[i].paid=='N'){
sum= sum+(cdata[i].cost*cdata[i].boxes);}
}
return sum;
}
int moneyReceivedPercent(int num, struct customer cdata[]){
int answ;
float money, moneyOut;
money = moneyReceived(num, cdata);
moneyOut = moneyOutstanding(num, cdata);
answ=(moneyOut/money)*100;
return answ;
}
Input white-space characters (as specified by the isspace function) are skipped, unless the specification includes a [, c, or n specifier. C11 §7.21.6.2 8
1) Add a space before "%c" to consume white-space. "%c" does not do it by itself. The other used specifiers do that even without a leading space.
2) Check the return value of *scanf() functions. Unless the return value if 6, in this case, do not trust that the scan completed successfully.
3) Limit string input by declaring a width one less than the size for '%s'. Code certainly should be prevented from overrunning the arrays.
for (i=0; i<numCustomers; i++){
// fscanf(file, "%s%s%s%d%f%c", &cdata[i].first, &cdata[i].last,
// &cdata[i].type, &cdata[i].boxes, &cdata[i].cost, &cdata[i].paid);
if (6 != fscanf(file, "%11s%11s%9s%d%f %c",
&cdata[i].first, &cdata[i].last, &cdata[i].type,
&cdata[i].boxes, &cdata[i].cost, &cdata[i].paid)) {
printf("Bad input!");
}
}

Storing Data Char Array C

[I write the program in C language]
I would like to read a txt file containing StudentName, Score, and Remarks
The pattern of the txt file looks like this:
1,Adam Lambert,60,C
2,Josh Roberts,100,A
3,Catherine Zetta,80,B
I would like to store each data, respectively into an array
So I would have 3 arrays (to store StudentName, Scores, and Grades)
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <math.h>
char* studName[99];
char* grade[99];
char* grades[100][10];
char buff[1024];
int scores[100];
int score;
int index;
int size;
int index = 0;
int sum = 0;
double average = 0;
int main()
{
FILE *file;
file = fopen("notepad.txt", "r");
if(file == NULL){
printf("Data does not Exist");
}
else {
while(((fgets(buff,1024,file))!=NULL))
{
size = strlen(buff);
buff[size] = 0;
sscanf(buff, "%d, %[^,],%d, %[^,]", &index, studName,&score, grade);
printf("Student Name: %s\n", studName);
printf("Score: %d\n", score);
printf("Remark: %s\n", grade);
scores[index-1] = score;
grades[index-1][10] = grade;
sum+=score;
index++;
}
}
fclose(file);
average = sum/index;
printf("\nThe total score of student is: %d\n", sum);
printf("\nThe sum of student is: %d\n", index);
printf("\nThe average of students' score is: %2f\n", average);
for(int i = 0; i<3; i++){
printf("Score: %d\n", scores[i]);
printf("\nThe Remark is: %s\n", grades[i][10]);
}
getch();
return 0;
}
The code above had successfully stored the scores in int array. I had not really good in C, so I do not know how to store the char array for StudentName and Grades.
The abovde code gives the result of Grades stored is only the last grade on the txt file (in this case is 'B').
Would you please tell me what do I have to fix in order to achieve my goal?
My goal basically will be pretty much like this:
StudentName array contains {"Adam Lambert", "Josh Roberts", "Catherine Zetta"}
Scores array contains {60,100,80}
Grades array contains {"C", "A", "B"}
Thank you very much.
Change studName and grade from "char *" to "char". You want a buffer, not an array of pointers.
At the top add:
char * StudentName[100];
When setting "scores" and "grades" add:
StudentName[index-1] = strdup(studName);
Also change the "10" in the following line, the array only dimensions 0-9.
grades[index-1][10] = grade;
Try this:
float len=sizeof(string);
Just gives you the static size of the array string which is an array of 20 pointers and each pointer is 4 bytes, 20 * 4 bytes = 80 that's why it's always 80.
while (fgets(string[i], BUFSIZE, fp)) {
i++;
len+=strlen(string[i]);
string[i] = (char *)malloc(BUFSIZE);
}
If your recore size is fixed then u can use this:
Where fr is a file pointer
fscanf(fr, "Index: %d StuName: %s Score: %d Grade: %s ", &index, sname, &score, grade);

Parsing data from ASCII formatted file in C

I am trying to do what's been done here Read co-ordinates from a txt files using C Program . The data that I am trying to input is in this format:
f 10 20 21
f 8 15 11
. . . .
f 11 12 25
The only difference in my point structure is that I have a an extra char to store the letter in the first column (which may or may not be the letter f). I guess im either declaring my char wrong, or I'm calling it in printf incorrectly. Either way, I only get the first line read and then my program terminates. Any ideas ?
Here is my MWE below
#define FILEPATHtri "/pathto/grid1DT.txt"
#define FILEPATHorg "/pathto/grid1.txt"
#define MAX 4000
#include <stdio.h>
#include <stdlib.h>
#include "math.h"
typedef struct
{
float x;
float y;
float z;
char t[1];
}Point;
int main(void) {
Point *points = malloc( MAX * sizeof (Point) ) ;
FILE *fp ;
fp = fopen( FILEPATHtri,"r");
int i = 0;
while(fscanf(fp, "%s %f %f %f ", points[i].t, &points[i].x, &points[i].y, &points[i].z ) == 4 )
{
i++;
}
fclose(fp);
int n;
for (n=0; n<=i; n++){
printf("%c %2.5f %2.5f %2.5f \n", points[i].t, points[n].x, points[n].y, points[n].z ); }
printf("There are i = %i points in the file \n And I have read n = %i points ",i,n);
return 0;
}
Since there's only 1 char in there, not a string just use a single char in your code:
char t;
}Point;
Then when you read it in:
while(fscanf(fp, "%c %f %f %f ", &points[i].t, &points[i].x, &points[i].y, &points[i].z ) == 4 )
{
I'll note that having an array of 1 char, at the end of a structure, sets you up for the struct hack which might not have been your intentions... A good reason to use just char t instead of char t[1]
Also this line:
for (n=0; n<=i; n++){
Should be
for (n=0; n<i; n++){
One last note... if you wanted to print the character out that you read in the prints at the bottom, you should be using n:
// note your previous code was points[i].t
printf("%c %f %f %f \n", points[n].t, points[n].x, points[n].y, points[n].z ); }
Check this
while(fscanf(fp, "%c %f %f %f ", points[i].t, &points[i].x, &points[i].y, &points[i].z ) == 4 )
{
i++;
}
fclose(fp);
int n;
for (n=0; n<i; n++){
printf("%c %2.5f %2.5f %2.5f \n", points[n].t, points[n].x, points[n].y, points[n].z ); }
printf("There are i = %i points in the file \n And I have read n = %i points ",i,n);
getch();
return 0;
}
modification are since only a single character is read %s modified to %c also in printf its not points[i].t its points[n].t . Also the limit checking in for loop is also corrected to n<i

Resources