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 2195 RRick 463
Recognized Expert Contributor
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)
Ganon11 3,652
Recognized Expert Specialist
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 replace just one item. I know I can get
the entire array, then save the whole thing (with a for loop and if
statements so that the changed data will be...
|
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 array at every page access to detect if the page is "free" so the
user can access it; when the user can access the page, the page is added to
the array...
|
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 then
print out the contents of the 2d array to the screen. I wil also need
to append data to the .dat file but mostly right now I'm worrying about...
|
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 to files as a large byte array.
This is a series of Lectures where there is a capacity for 8 instructors
with up to 8 lectures each. So a...
|
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 facing problems:
Assume that
FILE* filePointer;
unsigned char lineBuffer;
| |
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 to write a program in C++ that would allow the user to enter student information (matriculation number, name, status and mark), which was then stored...
|
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 with many configurations settings, once the options are set the
values will not change much.
So I thought it would be a good idea to move the data...
|
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 and then output
the contents of this array. The file seems to be parsed properly and
the array gets populated but when I output the array the last...
|
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 set of integers
stored in an array.It would be a great help if you could provide some
code for it.I tried the function fscanf but by that I am able...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |