syedshan
Advanced Member level 1
- Joined
- Feb 27, 2012
- Messages
- 463
- Helped
- 27
- Reputation
- 54
- Reaction score
- 26
- Trophy points
- 1,308
- Location
- Jeonju, South Korea
- Activity points
- 5,134
Dear all,
I am trying to read from a text file that has data for sine wave stored (generated using excel).
Any ways, the problem is when I am trying to read file I am not getting it done properly.
All I am getting is corrupted data or different data. What I think is that data has been read properly and stored
in memory, but problem is when I am reading from the memory. What solution is there for this problem
I have pasted my code and the small piece of sample data is also pasted
I am taking 256 samples (pasted at bottom)
Code
Where am I getting wrong. The following is the output that I am getting(which is wrong)
data from text file: from initial or first data
I am trying to read from a text file that has data for sine wave stored (generated using excel).
Any ways, the problem is when I am trying to read file I am not getting it done properly.
All I am getting is corrupted data or different data. What I think is that data has been read properly and stored
in memory, but problem is when I am reading from the memory. What solution is there for this problem
I have pasted my code and the small piece of sample data is also pasted
I am taking 256 samples (pasted at bottom)
Code
Code:
/* fopen example */
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
int main(void)
{
//Assigning variables
FILE * pFile;
long int *inp_buff;
//char *inp_buff;
long int temp[256];
//Allocating memory resources
inp_buff = ( long int *)malloc(sizeof( int)*256);
//Opening file
pFile = fopen ("sine1.txt","r");
//Reading from file
fread(inp_buff,4,256,pFile);
//Writing numbers into the integer array
for(int i=0; i<256; i++) {
printf("Number %d is %d\n",i, *(inp_buff + i));
}
free(inp_buff);
fclose(pFile);
return (0);
}
Where am I getting wrong. The following is the output that I am getting(which is wrong)
data from text file: from initial or first data
1000000
1024541
1049067
1073564
1098017
1122410
1146730
1170961
1195090
1219101
1242980
1266712
1290284
1313681
1336889
1359895
1382683
1405241
1427555
1449611
1471396
1492898
1514102
1534997
1555570
1575808
1595699
Last edited: