Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C++ read from file problem.

Status
Not open for further replies.

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
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)
Capture.PNGCapture.PNG

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:

can you try :

printf("Number %d is %d\n",i, ((int)*(inp_buff + i)));

instead of the original,
printf("Number %d is %d\n",i, *(inp_buff + i)); ?
 

Thank you for your reply.

But exactly the same result.

I also tried 'int' instead of 'long int' as well.

Bests,
Shan
 

Thank you for your reply.

But exactly the same result.

I also tried 'int' instead of 'long int' as well.

Bests,
Shan

memory is allocated on int basis as:
inp_buff = ( long int *)malloc(sizeof( int)*256);

sizeof (int)...
but each location is accessed as 'long int'.

can this be another try?
 

actually I also tried that thing too earlier, also I tried char, int, long int

What seems wrong to me is that I am not obtaining the data from memory all right.
coz I think data is stored in memory without problem, but ofcourse I am not sure since only way to verify is through this sort of program.

What do you say about this thing. Any idea!

Bests,
Shan
 

what your reading from the text file each line is not a integer! rather your reading a string of characters 7 or 8 digits followed by a newline.
you need to read each line, then convert it to a integer. BTW, if this is on a PC, I assume so, interger is same as long -32bits

so suggestion is

char linebuf[200];
int number;
....
fread(linebug,4,256,pFile);
sscanf(linebuf,"%d",&number);

printf("%d",number);
 
FREAD is used for read binary data as bytes from a file
to read numeric data in a text file use fscanf(), e.g.
Code:
/* fopen example */
#include <stdio.h>
#include <conio.h>
#include <stdlib.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( long 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++) {
fscanf(pFile, "%ld", (inp_buff + i));
printf("Number %d is %ld\n",i, *(inp_buff + i));
}

free(inp_buff);
fclose(pFile);
return (0);
}
if you are using C++ why not use the iostream library
https://www.cplusplus.com/reference/iostream/
 

With what Horace said above #include <iostream>, you can do something like this:

Code:
ifstream myfile;  (for input)
int x;
myfile.open("filename.txt")   

while(!myfile.empty())     //this will keep taking in information till the file runs out of data
{
  myfile >> x;              //sets data to a temp int value
  vector.push_back(x); // this will add your data into a vector which is easy to print
}

myfile.close

Same thing with output just use ofstream... a
 
Last edited by a moderator:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top