C++ :: Reading Data (Array Values) From TXT File

Mar 25, 2013

I am trying to read an array values from Tmin.txt file. I take array size from user viz. number of raw & column and then, read the following data from Tmin.txt file which look like this:

20 40 20 25 30
20 30 40 20 25
20 40 20 25 30
20 30 40 20 25
30 40 40 30 40

I am using getline, and then converting it to int by using atoi. But my code is not working.

Code :

// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

[Code] ....

View 5 Replies


ADVERTISEMENT

C :: Reading Values From CSV File Into Array

Jan 21, 2014

My problem is that I need to take a csv or excel file with tens of thousands of datapoints, write all points in a section of column to an array, perform calculations on them, and then write it back into another column.

I've been looking all over the internet for ways to do this easily. So far I have not found anything that I can follow and implement.Some codes have been slightly useful, but they aren't commented in a way I can understand. How to write code to take, for instance, lines from column b between 500-1000, write them to an array, and write another array 500 characters in column c. If that code could be commented, I have tried a few different techniques (counting commas), but haven't gotten anything to work.

View 9 Replies View Related

C :: Reading Values From File To First 2 Columns Of 2D Array / Assigning 0 To 3rd Column

Apr 4, 2014

I am attempting to read values from a file into a 2d array temp[31][2] (31 rows, 3 columns).I only want the values from the file to be read into the first two columns.I believe I am accomplishing that but when I go to print the array, I expect the first two columns to have the file data and the third column to have all zeros. The third column, however is printing such that the value is the next row/first column.

I'm not sure for instance why on the bottom loop for line 1 it doesn't print:

temp[0][0] temp[0][1] temp[0][2] 20 49 0 It instead prints: 20 49 1

Code:

#include <stdio.h> Code: #include <math.h>
FILE *inptr;
int main() {
int temp[31][2] = {0}, tempavg[31][2] = {0};
int i, j, k, sum;
inptr = fopen("ProgrammingProject14.txt", "r");

[code]....

View 8 Replies View Related

C :: Reading Data Into 2D Array From A File

Apr 29, 2013

I'm having issues reading to a 2d array from a file. When I try to read the data from my file into my matrix variable it simply doesn't read anything and leaves the variable unmodified. I've tried just reading the first piece of data in the main function and it doesn't work there either. I'm really perplexed at this point since I've never had an issue reading from a file before. Here's my relevant code:

Code:
int main(int argc, char** argv) {
double matrix[MINSIZE][MINSIZE]={0}, vectors[MAXSIZE][MINSIZE], ans[MAXSIZE][MINSIZE];
FILE * inFile;
inFile = fopen(FILENAME,"r");
if(inFile==NULL){
printf("File does not exist.

[Code] .....

View 4 Replies View Related

C :: Reading Data File Into Array Of Structures?

Nov 2, 2014

I'm reading the following file:

Osgood,Marcus 298542123 CHM FR mosgood@whatever.edu
Cronk,Melissa 873489021 BIO SR mcronk@whatever.edu
Pry,Seth 349908431 MTH SO spry@whatever.edu
Langlais,Susan 783323545 ME SR slanglais@whatever.edu
Davis,Nicole 987543345 PHY FR ndavis@whatever.edu

It's supposed to split it up into name, ID number, major, year, and email. The file reads it without any errors, and assigns name to the first part of the structure. However, ID gets assigned the ID, major, year, and email. Then Major gets assigned major, year, and email. Year gets assigned year and email, while email just gets assigned email. I don't know if it has something to do with the loop. For example, this is what I get what I print just the name and the ID.

Cronk,Melissa 873489021BIOSRmcronk@whatever.edu

Pry,Seth 349908431MTHSOspry@whatever.edu

Langlais,Susan 783323545ME

Davis,Nicole 987543345 PHYFRndavis@whatever.edu

Anyway. This is my function code for reading the array. I have it printing the ID number just to see if I can catch the errors earlier:

Code:
#include <stdio.h>
#include <string.h>
#include "functions.h"
void read_data(char filename[], studentinfo_t s[], int *size) {
FILE *infilep;
int countstudents = 0;
infilep = fopen("student_data", "r");

[Code].....

the name is reading correctly, but not anything else!

View 1 Replies View Related

C/C++ :: Reading BMP File - Getting Pixel Data Out Into 2D Array

Jan 18, 2015

I have managed to successfully extracted the BMP file header and file info but ran into troubles with getting the pixel data out into a 2d array.This is my function to read the data while i have another to write the data. The output does not match the input at all.

void BMPProgram::GetPixelData(std::ifstream & inStream) {
std::cout << "Entered function";
if(inStream.fail())
return;
//space allocated to reading in of padding
int paddingSpace = 0;

[Code] ....

PIXEL is a struct of 3 unsigned char containing values of RGB.

View 3 Replies View Related

C++ :: Create Matrix Of Data That Add Values Based On Reading Get From DVM

Apr 22, 2012

Ok I'm trying to create matrix of data that I can add values to based on a reading that I will get from a DVM. I'm using a 3d vector to do this. I need to keep track of the serial number, the day and within day I need to take two readings at different temps, this will continue for 7 days.

The rows of my 3d vector will be the days, the colums will be the serial numbers the depth will be the reading at the different temps.

What I need to do is compare the first element (days) and when it is greater then or equal to 4 I will perform calculations of the readings. So all I want to do is compare the first element of the vector, How do I do this?

Here is what I got

Code:
#include <vector>
typedef std::vector<double> Double1D;
typedef std::vector<Double1D> Double2D;
typedef std::vector<Double2D> Double3D;
#define HEIGHT 5
#define WIDTH 3

[Code]....

View 7 Replies View Related

C :: Reading Values From TXT File And It Hangs

Oct 5, 2013

I have a .txt with a 0 or 1 to each line of the txt file. there are many values to read. I am reading the values and calculating how many each reoccur in a sequence (run-length encode). If i only use say 50 values coming in it works, but once i do the hundreds there are in the file it hangs. i am not sure why.

Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

FILE *infile;
FILE *outfile;

void PrintBinary(int x,int y);

[Code] ....

View 3 Replies View Related

C++ :: Program Is Not Reading In Values From DAT File Correctly

May 20, 2013

We are supposed to split this lab into 3 files a .h and 2 .cpp's. The code I'll be talking about is going to be directed at the main.cpp file. Basically we are supposed to be able to enter as many classes into the program and when you are done the program is supposed to save the contents into a .dat file. This lab is working off of an older lab so I already have a .dat file I am trying to test with this program by reading it into my program when I first run it I prompt if the user wants to load previously entered courses and if they reply with a 'Y' the program will load the contents of the .dat file. Problem is right now when I load from the .dat file only the first class is correct. Every other class after that is just giberish. I've tried everything but can't get the restore part working. I also cannot get the save to .dat portion of this lab working either.Here is my code:

#include "Course.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;

[code]....

View 1 Replies View Related

C++ :: Reading Values In Order In File And Assign?

Apr 12, 2014

My game is a sort of RPG with stats, money etc. I just recently added a save/load system using writing to a file. I initially got writing to a text file to work, then I got loading to work too.I eventually was able to read numbers from the file and assign them to integer variables in order.

My issue was I wanted to check if a save file existed, if it did, load it up, if it did not, go to character creation. I had a lot of trouble with this and after trying different code snippets to work I finally got it to check if a file existed, and execute the appropriate code.

My issue now is my code USED to go through each entry and assign variables in order.

Like the first number in the text file would be for the variable money, and it would read it, assign to to int money and scroll to down to the next variable for player strength, assign to to playerstr variable and so on. After making the tweak for loading it no longer functions like this, and makes the last entry in the text file the value for everything.

Here's my code:

Save Code:

{
ofstream savegame;
savegame.open("C:/Sounds/savegame.dat", ios::trunc);
savegame << money << endl;
savegame << playerstr <<endl;

[Code]....

I barely get how this code works, how can I tweak it to go through the file in order and assign variables one at a time?

At the current moment, it assigned the playerstr value to both money and playerstr int. But the save file being created lists the correct values in order.

View 5 Replies View Related

C/C++ :: Reading Hex Values From File And Printing Them In Decimal

Dec 1, 2013

I am trying to read hex data from a file (not just hex values in text format). I was able to read it (02 EC) with "fread" into an char before but I need to change the Hex value into an integer in decimal.

I already read about strtol but I would prefer reading the hex Value into an integer.

Here is my code so far:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int itrack1[1];
int itrack2[1];

[Code] ....

View 11 Replies View Related

C++ :: Reading File Values By Passing Reference To A Function?

Oct 11, 2013

I am trying to read the file which has the values stored in the following pattern

2 4 10 103 2 504 .... and so on

I Have opened the file and then passed the opened file to another function to process it further.

here is my code

#include <iostream>
#include <fstream>
#include <vector>
#include <stdlib.h>

[Code].....

now the problem is when the control exits the "while loop" in the function "readingValues(std::ifstream& myFile)it goes straight to catch block ?

View 9 Replies View Related

Visual C++ :: Reading CSV File - Show Values In List?

Dec 26, 2012

I have problem reading CSV file, i found many solutions but i still got one problem. I need to read CSV file and then these values show in List Control and here i have problem. Idk how to display it in List?

View 4 Replies View Related

C++ :: Reading From File And Store Values Of Each Line In Multiple Variables

May 6, 2013

So I have this text file that I am trying to read from and store the values of each line in multiple variables.

Let's say my text file contains

AXSYZ3482 Tom 100 112

and my code below here works fine when the lines in the text file is separated by spaces.

while (fscanf(fp, "%s %s %d %d
", userID, name, &startLoc, &endLoc) != EOF) {
printf("%s %s %d %d
", userID, name, startLoc, endLoc);
}

But let's say my file was to look like this instead.

AXSYZ3482:Tom:100:112

But if i try this below...

while (fscanf(fp, "%s:%s:%d:%d
", userID, name, &startLoc, &endLoc) != EOF) {
printf("%s %s %d %d
", userID, name, startLoc, endLoc);
}

It seem to store the entire line in userID including the ":". I want to ignore the ":"'s and store everything in between in respective varibles in the order specified above.

So first string in userID, then ignore the :, then second string in name, and ignore the next :, and so forth. How I can accomplish this?

View 2 Replies View Related

C++ :: Reading Data File?

Nov 22, 2014

If i have a data,how to write the code for read this data?

View 1 Replies View Related

C/C++ :: File I/O - Reading Values From File

Jan 28, 2014

I am trying to write a program that will open a file with 2 points, take those points, determine what quadrant those points are in, and save the result back into the same file without truncating the data.

I'm having some trouble with my file I/O. I can't get my program to read the two values for X and Y that I put in the points.txt file. It simply uses whatever random number was used last time in that block of memory. In my points.txt file I put something like "4 -5" and it wont assign those numbers to my X and Y variables. I believe the issue is on line 25

#include <iostream>
#include <fstream>
using namespace std ;
int main() {
//defining X and Y
double X, Y ;

[Code] ....

View 14 Replies View Related

C++ :: Reading And Storing CSV File Data

Feb 21, 2013

I am working on a small c++ project where i read from a csv file with information in this format:

string,string,int
string,string,int
string,string,int

And all of the above information is one person. As is "John,Peter,23" is first name, last name, age. When there are multiple people in a csv file how can i parse through and separate the information in a string and int for later use?

View 4 Replies View Related

C++ :: Reading In And Initializing Data From File

Oct 30, 2014

Im familiar with fstream and know how to read in data, given that there is one type per line.

But I have a file that has multiple lines each containing a string (an address) and two double values (latitude and longitude).

Looking for support with the logic part of reading in data and initializing them to member variable.

View 1 Replies View Related

C++ :: Reading Variables From Data File

Feb 18, 2014

I can't get my program to read the rest of my variables from my data file it will only read the first two and my end of file won't work it keeps continuing on.

879.46
C 400.00
D 100.0
F 525.00
C 450.00
D 500.00
D 1000.00
C 2000.00
D 3000.00
D 3500.00
C 5500.00
C 500.00
B 200.00
C -235.00
D 250.00
H -500.00
D 500.00
E

That's my data it will only read the inital number and C and D but nothing else.

// develop algorithm to balance checking account make transactions

#include<iostream> // required for keyboard and screen I/O
#include<fstream> // required for external file stream
#include<iomanip>
#include<conio.h>
#include<string>

[Code] ......

View 5 Replies View Related

C++ :: WAV File Reading Wrong Data

Jan 19, 2015

I have been experimenting with a program that loads WAV file and tries to read 1s and 0s from audio file (binary coded information). So far I got working code, but the problem is that I'm not getting correct data. I do get somewhat similar data. The problem is that I get data that amplifies low amplitudes, so instead some noise and binary data I get lots of noise and hard to recognize binary data. My code is this

#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
// An unsigned char can store 1 Bytes (8bits) of data (0-255)

[Code] .....

This is wav file in binary viewer (after header)

0030 0032
00300035003200320035003A0048004A
004C0049004A00510049004300360038

and translated to decimal

48 50
4853505053587274
7673748173675456

and this is output from "wavosaur" program (and this one is correct)

0.001465
0.001526
0.001465
0.001617
0.001526
0.001526
0.001617
0.001770
0.002197
0.002258
0.002319
0.002228
0.002258
0.002472
0.002228
0.002045
0.001648
0.001709

You can see it's not the same. I do get the same data like I do from binary viewer, but I don't understand how they are translated to amplitude value. ? Where is the catch?

So this is not really question about c++, but about wav file structure and reading data.

View 2 Replies View Related

C++ :: Reading Data From Text File

Feb 25, 2013

I wanna read data from txt file with space divided each value like

1.00518 2.01903 3.01139 4.01343 5.02751 5.99913 7.00011 7.99851 8.99506 9.98015 10.9901 11.992 12.9923 13.9932 14.996 16.0034 17.012 18.0255 19.0366 20.0485 21.0505 22.0664 23.0455 24.0383 25.0374 26.0439 27.0378 28.0376 29.0576 30.066

I know the size of the data is 2*500.

In matlab I can use like

fid = fopen('example.txt');
data = fscanf(fid,'%f,',1499500);

how should I write in C++?

View 3 Replies View Related

C++ :: Multimaps - Reading File Data

Jan 16, 2013

I've got a problem with reading my file data but before I explain it I'll give some background. I'm programming a 2D Platformer using the SDL library for rendering.

My problem is that when reading in the data from the file it ignores all the data and just skips to the end of the function, I've attempted to debug it and the load function is getting called, the file is in the right directory and is being opened.

Here is my code:

Level.h
#include <vector>
#include <map>
#include <string>
#include <iostream>
#include <fstream>
#include "gameobject.h"
#include "miscobject.h"
struct LevelData {
// Multimap Prototype

[Code] ....

I'm not sure if it's the way I'm using multimaps. I should have mentioned that I put a break point in the multimap loops where it writes and inserts the objects and it doesn't seem to be hitting the functions, this is why I think there's something wrong with the multimaps.

View 3 Replies View Related

C++ :: Reading Data From Text File?

May 5, 2014

read some information from a text file. The program I'm working on is like a simple betting program.

What I need to read are:

match_code - team1 - team2 - odd1 - odd0 - odd2
139 Atletico Madrid - Real Madrid 2.853.40 2.35

But the spaces between datas are not known. We only know that both team names may contain more than one word and there is one space, exactly one dash and one more space (" – ") between team names.

Also match_code is an int and odds are double values.

while(getline(input, line)) {
istringstream ss(line);
ss >> match_code >> team1;
string temp;
while(ss >> temp) {

[Code] .....

In that missing part, I need to get the second word of team name if there is one; and the three odds that are odd1 odd0 and odd2.

View 2 Replies View Related

C++ :: Reading Data From A Text File?

Feb 10, 2015

Each line of the text file has first name, last name, and a salary. Something along the lines of this:

john doe 4000
bob miller 9000

I want my program to take the first name, last name, and salary of each line and assign them to strings. I have tried this so far:

while (inFile){
inFile >> firstName;
inFile >> lastName;
inFile >> grossPay;
cout << firstName << " " << lastName << " " << grossPay << endl;
}

When it outputs the names and the salary, the last line of the text file gets output twice by the program.

View 1 Replies View Related

C++ :: Reading Data From A File And Put It Into Several Vectors

Sep 26, 2013

I need debugging the code provided below, which reads data from a file and puts it into several vectors.

Something very strange happens: if I try to read the file up to the last line, I get an runtime error, while if I try to read the file up to the second last line, I do not get the error.

However, if I try to read the file up to the last line, it READS IT CORRECTLY, as you can see that it prints out the right values.

Following there is the code and the file that I am trying to read.

Code:

#include<stdio.h>
#include<iostream>
#include<fstream>
#include<vector>
#include<set>
#include<queue>
#include<cmath>
int main() {
std::ifstream fp;

[Code] ....

Following, there is the file "link1.dat" that I am trying to open.

10
1 1 8 2.171563e-06 1.837725e-02 3.939158e-05
2 3 17 1.067390e-05 3.612572e-02 1.047469e-04
3 2 9 1.166078e-05 3.280536e-02 3.207107e-05
4 4 26 1.236551e-05 3.143551e-02 2.054519e-04
5 7 18 3.430566e-05 2.821557e-02 1.109601e-04
6 12 18 9.582478e-06 2.847688e-02 4.161835e-04
7 12 43 5.816782e-06 4.352799e-02 4.141623e-04
8 12 16 2.036719e-05 4.194844e-02 2.629832e-04
9 12 11 1.829523e-05 3.096140e-02 4.543953e-04
10 11 47 2.563568e-05 4.001301e-02 3.140850e-04

View 5 Replies View Related

C/C++ :: Program Is Not Reading Data From DAT File

Feb 15, 2014

I'm not sure why my program isn't reading data from the dat file. I'm not sure if it's because I messed up my pointers, or I'm missing something. I know we usually use Infile >> blah >> blah >> blah; to get the data from the file, but my instructor showed an example that had the struct read it for you in this format and I could just call it with pointers.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <assert.h>
using namespace std;
using namespace System;
typedef int Integer;

[Code] ....

View 12 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved