473,387 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

File reading into array

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 2180
RRick
463 Expert 256MB
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 Expert 2GB
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
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
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
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...
5
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...
29
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...
1
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...
9
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...
2
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...
9
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...
7
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...
4
by: MikeJ | last post by:
make a While loop ofs = TextFileServer("somefile") string srow while (ofs=false) { srow=ofs.getRow(); Console.Writeline(srow); }
13
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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
0
BarryA
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...
1
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...
0
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...
0
marktang
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,...
0
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...

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.