473,548 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

File reading into array

3 New Member
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...
Aug 28 '07 #1
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)
Aug 28 '07 #2
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!
Aug 28 '07 #3
nzjohnc
3 New Member
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.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include<conio.h>
  3.  
  4. //int survey[6][6];
  5. //int count;
  6. void main()
  7. {
  8. FILE *fp;
  9. fp=fopen("h:\\nfworks\\custsurvey.txt","r");
  10. //int i,j; 
  11. //int sum_f,sum_m;
  12. char gender;
  13. int age,read;
  14.  
  15. {
  16. //for(count=0;count<36;count++)
  17. {
  18. while
  19. (fscanf(fp,"%c %d %d",&gender,&age,&read)!=EOF);
  20.  
  21.  
  22. //if(gender='F')
  23. {
  24.  
  25.  
  26. //sum_f+=i;
  27. //printf("%d",sum_f);
  28.  
  29. }
  30. }
  31.  printf("test ok....\n");
  32.  if(fclose(fp)==EOF)
  33.  puts("Cannot close file..");
  34.  }
  35.  puts("\n\n --------------");
  36.  getch();
  37.  }
  38.  
Aug 29 '07 #4
nzjohnc
3 New Member
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.
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4. #include <stdio.h>
  5. #include <conio.h>
  6.  
  7.  
  8. #define Typeone(Gender) (Gender=='F') ? 0 : 1
  9.  
  10.  
  11.  
  12. int Category2,CustomerType[2][3][6],LeadTypeCustomers[6];
  13.  
  14. void Read();
  15. void TypeTwo(int Age);
  16. void Report();
  17. void PrintReportHeading(FILE *fp);
  18. void PrintSideHeadings(FILE *fp,int i);
  19. void PrintSurveyDetails(FILE *fp,int i,int j,int k);
  20. void PrintTotalSection(FILE *fp,int LeadGrandTotal);
  21. void PrintLeadTypeCustomerTotal(FILE *fp,int i);
  22.  
  23. void main()
  24. {
  25.  
  26.     Read();
  27.     Report();
  28.  
  29. }
  30.  
  31. void Read()
  32. {
  33.  
  34.     char Gender,NewLine;
  35.  
  36.  
  37.  
  38.     int Age,Lead,Category1,Category3=0;
  39.  
  40.  
  41.  
  42.     FILE *fp = fopen("h:\\nfworks\\custsurvey.dat","r");
  43.  
  44.     if(fp == NULL)
  45.     {
  46.  
  47.  
  48.  
  49.         printf("Error. Can't open the file");
  50.         getch();
  51.  
  52.     }
  53.     else
  54.     {
  55.  
  56.  
  57.         while(fscanf(fp,"%c %d %d",&Gender,&Age,&Lead)!=EOF)
  58.         {
  59.  
  60.  
  61.             Category1 =Typeone(Gender);
  62.  
  63.  
  64.  
  65.             TypeTwo(Age);
  66.  
  67.             Category3 = Lead-1;
  68.  
  69.  
  70.  
  71.             CustomerType[Category1][Category2][Category3] = CustomerType[Category1][Category2][Category3] + 1;
  72.  
  73.  
  74.             fscanf(fp,"%c",&NewLine);   
  75.  
  76.         }
  77.  
  78.         if(fclose(fp)==EOF)
  79.         {
  80.  
  81.  
  82.  
  83.         printf("\nError. Can't close the file");
  84.             getch();
  85.  
  86.         }
  87.  
  88.     }
  89.  
  90. }
  91.  
  92. void TypeTwo(int Age)
  93. {
  94.  
  95.     if(Age<25)
  96.     {
  97.  
  98.         Category2 = 0;
  99.  
  100.     }
  101.     else if(Age<=40)
  102.     {
  103.  
  104.         Category2 = 1;
  105.  
  106.     }
  107.     else
  108.     {
  109.  
  110.         Category2 = 2;
  111.  
  112.     }
  113.  
  114. }
  115.  
  116. void Report()
  117. {
  118.  
  119.  
  120.     int Count1,Count2,Count3,LeadTotal,LeadGrandTotal =0;
  121.  
  122.  
  123.     FILE *fp = fopen("h:\\nfworks\\project_test.txt","w");
  124.  
  125.     if(fp == NULL)
  126.     {
  127.  
  128.  
  129.         printf("Error. Can't open the file");
  130.         getch();
  131.  
  132.     }
  133.     else
  134.     {
  135.  
  136.         PrintReportHeading(fp);
  137.  
  138.         for(Count1=0;Count1<6;Count1++)
  139.         {
  140.  
  141.  
  142.             PrintSideHeadings(fp,Count1);
  143.  
  144.             LeadTotal=0;
  145.  
  146.             for(Count2=0;Count2<2;Count2++)
  147.             {
  148.  
  149.  
  150.  
  151.  
  152.                 for(Count3=0;Count3<3;Count3++)
  153.                 {
  154.  
  155.  
  156.  
  157.                     PrintSurveyDetails(fp,Count1,Count2,Count3);
  158.  
  159.                     LeadTotal = LeadTotal + CustomerType[Count2][Count3][Count1];
  160.  
  161.  
  162.                     LeadTypeCustomers[Count1] = LeadTypeCustomers[Count1]
  163.                                                                 +
  164.                                             CustomerType[Count2][Count3][Count1];
  165.  
  166.  
  167.                 }
  168.  
  169.             }
  170.  
  171.             LeadGrandTotal = LeadTotal+LeadGrandTotal;
  172.  
  173.             fprintf(fp,"\t %d",LeadTotal);
  174.  
  175.         }
  176.  
  177.  
  178.  
  179.         PrintTotalSection(fp,LeadGrandTotal);
  180.  
  181.         if(fclose(fp))
  182.         {
  183.  
  184.  
  185.             printf("Error. Can't close the file.");
  186.             getch();
  187.  
  188.         }
  189.  
  190.     }
  191.  
  192. }
  193.  
  194. void PrintReportHeading(FILE *fp)
  195. {
  196.  
  197.     fprintf(fp,"\t\t\t    CUSTOMER SURVEY REPORT\n");
  198.     fprintf(fp,"\t\t\t    **********************\n");
  199.     fprintf(fp,"\t\t      FEMALES\t\t\tMALES\n");
  200.     fprintf(fp,"\t      ***********************  ***********************\n");
  201.     fprintf(fp,"\t      younger  range   older   younger  range   older\n");
  202.     fprintf(fp,"\t      than 25 25 - 40 than 40  than 25 25 - 40 than 40\n");
  203.     fprintf(fp,"    LEAD TYPE ***********************  *********************** TOTAL\n");
  204.     fprintf(fp,"    *********\t\t\t\t\t\t       *****\n\n");
  205.  
  206. }
  207.  
  208.  
  209. void PrintSideHeadings(FILE *fp,int i)
  210. {
  211.  
  212.     switch (i)
  213.     {
  214.  
  215.         case (0):fprintf(fp,"       REPEAT");
  216.                  break;
  217.         case (1):fprintf(fp,"\n\nTELEVISION AD");
  218.                  break;
  219.         case (2):fprintf(fp,"\n\n NEWSPAPER AD");
  220.                  break;
  221.         case (3):fprintf(fp,"\n\n     RADIO AD");
  222.                  break;
  223.         case (4):fprintf(fp,"\n\nWORD OF MOUTH");
  224.                  break;
  225.         case (5):fprintf(fp,"\n\n\tOTHER");
  226.                  break;
  227.     }
  228.  
  229. }
  230.  
  231. void PrintSurveyDetails(FILE *fp,int i,int j,int k)
  232. {
  233.  
  234.  
  235.     if(j==0)
  236.     {
  237.  
  238.         fprintf(fp,"\t %3d",CustomerType[j][k][i]);
  239.  
  240.     }
  241.     else 
  242.     {
  243.  
  244.         fprintf(fp,"\t%3d",CustomerType[j][k][i]);
  245.  
  246.     }
  247.  
  248.   }
  249.  
  250.  
  251. void PrintTotalSection(FILE *fp,int LeadGrandTotal)
  252. {
  253.  
  254.  
  255.     int i,AgeRangeGrantTotal = 0;
  256. fprintf(fp,"\n\t*****  ****    ****    ****     ****    ****    ****   ****\n");
  257.  
  258.     fprintf(fp,"\tTOTAL");
  259.  
  260.     for(i=0;i<6;i++)
  261.     {
  262.  
  263.  
  264.         /*PrintLeadTypeCustomerTotal(fp,i);*/
  265.  
  266.         AgeRangeGrantTotal =AgeRangeGrantTotal + LeadTypeCustomers[i];
  267.  
  268.     }
  269.  
  270.  
  271.  
  272.     //if(LeadGrandTotal==AgeRangeGrantTotal)
  273.     {
  274.  
  275.         fprintf(fp,"\t%d",AgeRangeGrantTotal);
  276.  
  277.     }
  278.  
  279.  
  280.      fprintf(fp,"\n\n\t*****  ****    ****    ****     ****    ****    ****   ****\n");
  281.  
  282. }
  283.  
  284. void PrintLeadTypeCustomerTotal(FILE *fp,int i)
  285. {
  286.  
  287.  
  288.  
  289.     if(i == 0)
  290.     {
  291.  
  292.         fprintf(fp,"  %4d",LeadTypeCustomers[i]);
  293.  
  294.     }
  295.     else if((i == 1)||(i == 2))
  296.     {
  297.  
  298.         fprintf(fp,"    %4d",LeadTypeCustomers[i]);
  299.  
  300.     }
  301.     else
  302.     {
  303.  
  304.         fprintf(fp,"\t%4d",LeadTypeCustomers[i]);
  305.  
  306.     }
  307.  
  308. }
  309.  
  310.  
  311.  
  312.  
Sep 17 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
6844
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...
5
1516
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...
29
10386
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...
1
1761
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...
9
5193
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;
2
1734
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...
9
2181
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...
7
3578
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...
4
2114
by: MikeJ | last post by:
make a While loop ofs = TextFileServer("somefile") string srow while (ofs=false) { srow=ofs.getRow(); Console.Writeline(srow); }
13
10380
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...
0
7444
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...
0
7711
Oralloy
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. ...
0
7954
jinu1996
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...
1
7467
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...
0
6039
agi2029
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...
0
5085
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...
0
3497
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...
1
1054
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
755
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.