Hi all,
just started c ,need some help for storing values in arrays.I have opened the file for reading using fscanf,cos its got only three fields,but when i try to get the values to array,I'm getting the address values.If any one could give some advice on the procedure or concept of getting the values from a text file and storing them in arrays for further calculations would be a great help...
4 2180
What kind of values are you trying to read in? Integers? Strings?
A snippet of the code that doesn't work would help. (Please use [ code]...[/code] around the source code)
Please use a clearer title in the future. The title "c programming..." is very broad and does not provide other users with any idea of your question. A clearer title will help users identify your problem quicker and, thus, be able to give you more help.
Thanks!
Hi all,
just started c ,need some help for storing values in arrays.I have opened the file for reading using fscanf,cos its got only three fields,but when i try to get the values to array,I'm getting the address values.If any one could give some advice on the procedure or concept of getting the values from a text file and storing them in arrays for further calculations would be a great help...
c programming help!!
This is only the reading part that has been done.The file has a one character and two integer colums.The idea is to sum the values based on conditions(eg gender ,age group,preferred media).eg of data in the file F 29 1(200 of them),where F correspond to gender 29 to age and 1 to choice of media.I'm making an attempt to get the values into a 2 dimensional array,but need some help regarding the correct procedure to get the values into the array to start with.All tips welcome
The items after //are part of the ongoing experiment but devoid of them ,the file opens for reading. - #include <stdio.h>
-
#include<conio.h>
-
-
//int survey[6][6];
-
//int count;
-
void main()
-
{
-
FILE *fp;
-
fp=fopen("h:\\nfworks\\custsurvey.txt","r");
-
//int i,j;
-
//int sum_f,sum_m;
-
char gender;
-
int age,read;
-
-
{
-
//for(count=0;count<36;count++)
-
{
-
while
-
(fscanf(fp,"%c %d %d",&gender,&age,&read)!=EOF);
-
-
-
//if(gender='F')
-
{
-
-
-
//sum_f+=i;
-
//printf("%d",sum_f);
-
-
}
-
}
-
printf("test ok....\n");
-
if(fclose(fp)==EOF)
-
puts("Cannot close file..");
-
}
-
puts("\n\n --------------");
-
getch();
-
}
-
Reading Text File and producing output in c language.
Hi Guys here is what I have done so far.It still has a problem of not counting the vertical colums that is the sum total of different age groups.Could you give some idea on where to get the for loop going for going through all the elements of the array.I am stuck with that part now.Please help. -
-
-
-
#include <stdio.h>
-
#include <conio.h>
-
-
-
#define Typeone(Gender) (Gender=='F') ? 0 : 1
-
-
-
-
int Category2,CustomerType[2][3][6],LeadTypeCustomers[6];
-
-
void Read();
-
void TypeTwo(int Age);
-
void Report();
-
void PrintReportHeading(FILE *fp);
-
void PrintSideHeadings(FILE *fp,int i);
-
void PrintSurveyDetails(FILE *fp,int i,int j,int k);
-
void PrintTotalSection(FILE *fp,int LeadGrandTotal);
-
void PrintLeadTypeCustomerTotal(FILE *fp,int i);
-
-
void main()
-
{
-
-
Read();
-
Report();
-
-
}
-
-
void Read()
-
{
-
-
char Gender,NewLine;
-
-
-
-
int Age,Lead,Category1,Category3=0;
-
-
-
-
FILE *fp = fopen("h:\\nfworks\\custsurvey.dat","r");
-
-
if(fp == NULL)
-
{
-
-
-
-
printf("Error. Can't open the file");
-
getch();
-
-
}
-
else
-
{
-
-
-
while(fscanf(fp,"%c %d %d",&Gender,&Age,&Lead)!=EOF)
-
{
-
-
-
Category1 =Typeone(Gender);
-
-
-
-
TypeTwo(Age);
-
-
Category3 = Lead-1;
-
-
-
-
CustomerType[Category1][Category2][Category3] = CustomerType[Category1][Category2][Category3] + 1;
-
-
-
fscanf(fp,"%c",&NewLine);
-
-
}
-
-
if(fclose(fp)==EOF)
-
{
-
-
-
-
printf("\nError. Can't close the file");
-
getch();
-
-
}
-
-
}
-
-
}
-
-
void TypeTwo(int Age)
-
{
-
-
if(Age<25)
-
{
-
-
Category2 = 0;
-
-
}
-
else if(Age<=40)
-
{
-
-
Category2 = 1;
-
-
}
-
else
-
{
-
-
Category2 = 2;
-
-
}
-
-
}
-
-
void Report()
-
{
-
-
-
int Count1,Count2,Count3,LeadTotal,LeadGrandTotal =0;
-
-
-
FILE *fp = fopen("h:\\nfworks\\project_test.txt","w");
-
-
if(fp == NULL)
-
{
-
-
-
printf("Error. Can't open the file");
-
getch();
-
-
}
-
else
-
{
-
-
PrintReportHeading(fp);
-
-
for(Count1=0;Count1<6;Count1++)
-
{
-
-
-
PrintSideHeadings(fp,Count1);
-
-
LeadTotal=0;
-
-
for(Count2=0;Count2<2;Count2++)
-
{
-
-
-
-
-
for(Count3=0;Count3<3;Count3++)
-
{
-
-
-
-
PrintSurveyDetails(fp,Count1,Count2,Count3);
-
-
LeadTotal = LeadTotal + CustomerType[Count2][Count3][Count1];
-
-
-
LeadTypeCustomers[Count1] = LeadTypeCustomers[Count1]
-
+
-
CustomerType[Count2][Count3][Count1];
-
-
-
}
-
-
}
-
-
LeadGrandTotal = LeadTotal+LeadGrandTotal;
-
-
fprintf(fp,"\t %d",LeadTotal);
-
-
}
-
-
-
-
PrintTotalSection(fp,LeadGrandTotal);
-
-
if(fclose(fp))
-
{
-
-
-
printf("Error. Can't close the file.");
-
getch();
-
-
}
-
-
}
-
-
}
-
-
void PrintReportHeading(FILE *fp)
-
{
-
-
fprintf(fp,"\t\t\t CUSTOMER SURVEY REPORT\n");
-
fprintf(fp,"\t\t\t **********************\n");
-
fprintf(fp,"\t\t FEMALES\t\t\tMALES\n");
-
fprintf(fp,"\t *********************** ***********************\n");
-
fprintf(fp,"\t younger range older younger range older\n");
-
fprintf(fp,"\t than 25 25 - 40 than 40 than 25 25 - 40 than 40\n");
-
fprintf(fp," LEAD TYPE *********************** *********************** TOTAL\n");
-
fprintf(fp," *********\t\t\t\t\t\t *****\n\n");
-
-
}
-
-
-
void PrintSideHeadings(FILE *fp,int i)
-
{
-
-
switch (i)
-
{
-
-
case (0):fprintf(fp," REPEAT");
-
break;
-
case (1):fprintf(fp,"\n\nTELEVISION AD");
-
break;
-
case (2):fprintf(fp,"\n\n NEWSPAPER AD");
-
break;
-
case (3):fprintf(fp,"\n\n RADIO AD");
-
break;
-
case (4):fprintf(fp,"\n\nWORD OF MOUTH");
-
break;
-
case (5):fprintf(fp,"\n\n\tOTHER");
-
break;
-
}
-
-
}
-
-
void PrintSurveyDetails(FILE *fp,int i,int j,int k)
-
{
-
-
-
if(j==0)
-
{
-
-
fprintf(fp,"\t %3d",CustomerType[j][k][i]);
-
-
}
-
else
-
{
-
-
fprintf(fp,"\t%3d",CustomerType[j][k][i]);
-
-
}
-
-
}
-
-
-
void PrintTotalSection(FILE *fp,int LeadGrandTotal)
-
{
-
-
-
int i,AgeRangeGrantTotal = 0;
-
fprintf(fp,"\n\t***** **** **** **** **** **** **** ****\n");
-
-
fprintf(fp,"\tTOTAL");
-
-
for(i=0;i<6;i++)
-
{
-
-
-
/*PrintLeadTypeCustomerTotal(fp,i);*/
-
-
AgeRangeGrantTotal =AgeRangeGrantTotal + LeadTypeCustomers[i];
-
-
}
-
-
-
-
//if(LeadGrandTotal==AgeRangeGrantTotal)
-
{
-
-
fprintf(fp,"\t%d",AgeRangeGrantTotal);
-
-
}
-
-
-
fprintf(fp,"\n\n\t***** **** **** **** **** **** **** ****\n");
-
-
}
-
-
void PrintLeadTypeCustomerTotal(FILE *fp,int i)
-
{
-
-
-
-
if(i == 0)
-
{
-
-
fprintf(fp," %4d",LeadTypeCustomers[i]);
-
-
}
-
else if((i == 1)||(i == 2))
-
{
-
-
fprintf(fp," %4d",LeadTypeCustomers[i]);
-
-
}
-
else
-
{
-
-
fprintf(fp,"\t%4d",LeadTypeCustomers[i]);
-
-
}
-
-
}
-
-
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: melanieab |
last post by:
Hi,
I'm trying to store all of my data into one file (there're about 140 things
to keep track of). I have no problem reading a specific string from the
array file, but I wasn't sure how to...
|
by: Fabio R. |
last post by:
To support a webfarm scenario, I'd like to store a global array (serialized)
in a file on a network share.
In this array there is a list of pages "locked" by other users so I need to
read this...
|
by: yourmycaffiene |
last post by:
Okay, this if my first post so go easy on me plus I've only been using
C for a couple of weeks. I'm working on a program currently that
requires me to read data from a .dat file into a 2d array and...
|
by: Galen Somerville |
last post by:
And yet another VB6 to VB2005 problem. All helpful suggestions appreciated.
As you can see in the code below, my structures use fixed length strings and
known array sizes. Consequently I can save...
|
by: Adi |
last post by:
Hello eveyone,
I wanna ask a very simple question here (as it was quite disturbing
me for a long time.)
My problem is to read a file line by line. I've tried following
implementations but still...
|
by: GeoUK |
last post by:
Hi All,
New member here with a bit of problem. I have read the FAQ's and searched text books but still cannot solve the problem that I have.
As part of a course I am doing at University I had...
|
by: FFMG |
last post by:
In my site I have a config table, (MySQL), with about 30 entries; the
data is loaded on every single page load. This is not the only call to
the db, (we do a total of about 8 calls to the db).
As...
|
by: theballz |
last post by:
Hi,
I am learning c programming and come across a problem i cant seem to
solve. I have a file which i wish to parse and put certain lines
(which do not contain a hash character) into an array...
|
by: MikeJ |
last post by:
make a While loop
ofs = TextFileServer("somefile")
string srow
while (ofs=false)
{
srow=ofs.getRow();
Console.Writeline(srow);
}
|
by: rohit |
last post by:
Hi All,
I am new to C language.I want to read integers from a text file and
want to do some operation in the main program.To be more specific I
need to multiply each of these integers with another...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |