473,387 Members | 1,890 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.

filling numbers from a file to an array

hii,
i am working in c and my problem is that i have created a file which contains numbers,i want to store these numbers in an array.,please guide me how to go about it this is the part of code that i have written ,it is showing hundred numbers but all values are same and it shows 277 866 100 times.

while((ch=fgetc(fp)!=NULL)
{
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
image[i][j]=ch;
printf("%d %d",image[i][j]);
}
Mar 27 '07 #1
12 2823
horace1
1,510 Expert 1GB
put the read in the loops?
Expand|Select|Wrap|Line Numbers
  1.  
  2. {
  3. for(i=0;i<10;i++)
  4. for(j=0;j<10;j++)
  5. {
  6. ch=fgetc(fp);
  7. image[i][j]=ch;
  8. printf("%d %d",image[i][j]);
  9. }
fegtc() reads characters, should you use
Expand|Select|Wrap|Line Numbers
  1. scanf("%d", image[i][j]);
Mar 27 '07 #2
put the read in the loops?
Expand|Select|Wrap|Line Numbers
  1.  
  2. {
  3. for(i=0;i<10;i++)
  4. for(j=0;j<10;j++)
  5. {
  6. ch=fgetc(fp);
  7. image[i][j]=ch;
  8. printf("%d %d",image[i][j]);
  9. }
fegtc() reads characters, should you use
Expand|Select|Wrap|Line Numbers
  1. scanf("%d", image[i][j]);

sorry this didn't worked if possible can you give any other solution to this
Mar 27 '07 #3
i am working in c and i have created a file which contains 100 numbers.i want to store these numbers in an array like a[10][10].how to do this.please give the code to do tjis.i have displayed the numbers on the screen but after that i want to store the numbers in an array.please help its urgent.
Mar 27 '07 #4
i am working in c and i have created a file which contains 100 numbers.i want to store these numbers in an array like a[10][10].how to do this.please give the code to do tjis.i have displayed the numbers on the screen but after that i want to store the numbers in an array.please help its urgent.
the code is as follows:
#include<stdio.h>
#include<conio.h>
main()
{
FILE *fp;
int i,,j,a[10][10];
char ch;
fp=fopen("abb.txt","r");
if(fp==NULL)
{
printf("Cannot open file");
exit();
}
while((ch=fgetc(fp))!=EOF)
{
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
ch=fgetc(fp);
image[i][j]=ch;
printf("%d %d",image[i][j]);
}
}
fclose(fp);
}
:
Mar 27 '07 #5
r035198x
13,262 8TB
i am working in c and i have created a file which contains 100 numbers.i want to store these numbers in an array like a[10][10].how to do this.please give the code to do tjis.i have displayed the numbers on the screen but after that i want to store the numbers in an array.please help its urgent.
the code is as follows:
#include<stdio.h>
#include<conio.h>
main()
{
FILE *fp;
int i,,j,a[10][10];
char ch;
fp=fopen("abb.txt","r");
if(fp==NULL)
{
printf("Cannot open file");
exit();
}
while((ch=fgetc(fp))!=EOF)
{
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
ch=fgetc(fp);
image[i][j]=ch;
printf("%d %d",image[i][j]);
}
}
fclose(fp);
}
:
1.) Do not double post
2.)Post code using code tags
3.)Haven't you already put the values in the array called image?
Mar 27 '07 #6
Ganon11
3,652 Expert 2GB
Why do you need the while loop? It is supposed to execute until the end of file, but it works by reading in a character - then, in your loop, you read in a new character, meaning the first is lost. You should just have the for...loops, no while loop.

Also, in your printf call, you use %d twice to indicate you are printing two values, but you only include a[i][j] to be printed - should you only have one %d?
Mar 27 '07 #7
gpraghuram
1,275 Expert 1GB
HI,
Can you provide me a sample of your input file...
i can give a try to help you with code.
Thanks
Raghuram
Mar 27 '07 #8
HI,
Can you provide me a sample of your input file...
i can give a try to help you with code.
Thanks
Raghuram
HII.
my text file contains numbers every row having 10 numbers.
as given below


3 3 4 5 6 6 .3
3 3 3 3 6 3..3
2
2 .............
2 3 4 5 6 7 3
Mar 28 '07 #9
gpraghuram
1,275 Expert 1GB
Hi,
The problem with your code was reading the integer with fgetc
try this one...
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.     int read_arr[10][10];
  6.     char ch;
  7.     int i=0,j=0;
  8.     char read_array[200];
  9.  
  10.     FILE *fptr=fopen("ip_3.txt","r");
  11.     if(fptr != NULL)
  12.     {
  13.         while(!feof(fptr))
  14.         {
  15.             for(j=0;j<=10;j++)
  16.             {
  17.                 fscanf(fptr,"%d ",&(read_arr[i][j]));
  18.                 printf("%d ",i,j,read_arr[i][j]);
  19.             }
  20.             printf("\n");
  21.             i++;
  22.         }
  23.         fclose(fptr);
  24.     }
  25. }
  26.  
Thanks
Raghuram
Mar 28 '07 #10
Hi,
The problem with your code was reading the integer with fgetc
try this one...
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.     int read_arr[10][10];
  6.     char ch;
  7.     int i=0,j=0;
  8.     char read_array[200];
  9.  
  10.     FILE *fptr=fopen("ip_3.txt","r");
  11.     if(fptr != NULL)
  12.     {
  13.         while(!feof(fptr))
  14.         {
  15.             for(j=0;j<=10;j++)
  16.             {
  17.                 fscanf(fptr,"%d ",&(read_arr[i][j]));
  18.                 printf("%d ",i,j,read_arr[i][j]);
  19.             }
  20.             printf("\n");
  21.             i++;
  22.         }
  23.         fclose(fptr);
  24.     }
  25. }
  26.  
Thanks
Raghuram
thanks for your program but
sorry this too didn't work.
your program shows the output :
0000000000
1111111111
2222222222
3333333333
4444444444
5555555555
6666666666
7777777777
8888888888
9999999999
but i want that it should display the numbers that i have saved in a notepad file.
and also i didn't get why you initialised the same array twice with different datatypes.it is giving error.array initialised more than once..
Mar 28 '07 #11
Ganon11
3,652 Expert 2GB
thanks for your program but
sorry this too didn't work.
This is why you shouldn't copy & paste someone else's code without checking it yourself first. There are several things wrong with this code. For one, the for...loop starts j at 0 and has an end condition of j <= 10, meaning there will be a total of 11 executions. This will make you run out of numbers and also read data past the bounds of read_arr. Also, the printf call gives room for one output, but tries to output i, j, and read_arr[i][j].
Mar 28 '07 #12
gpraghuram
1,275 Expert 1GB
Hi,
The problem was the printf statement
printf("%d ",i,j,read_arr[i][j]);

Change this to
printf("%d ",read_arr[i][j]);

Thanks
Raghuram
Mar 29 '07 #13

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

Similar topics

5
by: rob | last post by:
hey every1, I've got alot of data to write out to file and it's all just 1's and 0's. It's all stored in 2 dimensional arrays of width 32 and varying height. At the moment it's all just...
0
by: August1 | last post by:
This is a follow up to using these functions to produce lottery numbers to an outfile, then read the numbers from the file to the screen. Although other methods are certainly available. ...
3
by: MSDousti | last post by:
Hello I want to write a C# (or VB.NET) program which reads the structure of a PE (odinary win32 executable) file. These files have a long header (512 bytes or so). The definition of the header...
3
by: Serdar C | last post by:
hello there, i am writing a program that generates random numbers and shows the numbers on a (x,y) chart.. i decided to use crystal reports to draw the chart and a dataset.readxml to read data.....
12
by: pjhyett | last post by:
standard 2d array filling with increasing numbers for rows and columns: for(int i=0;i<n;i++) for(int j=0;j<n;j++) a = i + j; problem is it's O(n^2). I'm looking for a method to decrease the...
9
by: srikanth | last post by:
i have a text file like below, test.txt file (actually my test file file is with 10000 lines but here i tested with 3 lines) 3 06.09.2006 16:37:25 3 06.09.2006 16:40:02 3 06.09.2006 16:42:31...
10
by: hey77 | last post by:
If I wish to have an array filled with a set number of user inputed numbers how would i go about this? Also I could possibly then seek out how many times a certain number was input into the array? ...
5
by: John | last post by:
How can I fill an array randomly so it contains a certian range of numbers (1 - 100 for example) ? My Goal is to generate a set of numbers in random order.
5
by: lim4801 | last post by:
I am currently in doing a program which is given by my tutor: Contemplate that you are working for the phone company and want to sell "special" phone numbers to companies. These phone numbers are...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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.