C++ :: Unable To Read Data File From Two Dimensional Array

Apr 27, 2014

I am new to C++ and I am having an issue reading in a 2 Dimensional array from a data file. I am very close to reading it in perfectly except for one issue, the loop is ignoring the first value from the data file.

This is the code I have so far:

#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <functional>
#include <conio.h>
#include <iomanip>
#include <string>
#include <fstream>

[Code]...

This is the values from the data file:

300 450 500 210
510 600 750 400
627 100 420 430
530 621 730 530
200 050 058 200
100 082 920 290

When the Program shows my array it comes up as:

450 500 210 510
600 750 400 627
100 420 430 530
621 730 530 200
050 058 200 100
082 920 290 0

Every array location is moved up one, like the program just completely ignored the 300 at the beginning.

View 1 Replies


ADVERTISEMENT

C :: Unable To Read Data From Text File And Print It

May 12, 2014

I am having trouble in reading data from my text file and print it out exactly like how it looks like in the text file. The problem im having is it will not read the last y Coordinates of the point. it keep reading the second last point for y coordinates which is wrong.

my text file is
0.0 0.0
0.0 1.0
1.0 0.0(but it read until here)
0.0 0.0(suppose to read the last point which is here)

For your information, this is my 1st year degree assignment in C programming. It is to create a program which can read text file (manually create) and print it out in a program and calculate the area for the polygon using ADT function ( .c and .h files)

*This is the code for my read file function*

Basically this accepts a Polygon and a file pointer as parameters. It reads the polygon point data from the file, pass the read data to plg_new() to create a new Polygon and returns the new Polygon created.

Code:

polygon *plg_read(polygon *New_polygon, FILE *Coord) {
int i;
int numberofvertices=0;
int count=0;
char filename[50];
double xCoor[50], yCoor[50];

[Code]....

This is the second function my polygon new code. This ADT function basically creates a new Polygon with malloc(), initialize all ADT data members with its parameter values and returns the Polygon.

Code:

polygon *plg_new(double *xCoordinates, double *yCoordinates, int numberOfVertices) {
int x;
polygon *New_poly = (polygon *)malloc(sizeof (polygon));
if(New_poly->xCoordinates == NULL || New_poly->yCoordinates == NULL) {
free(New_poly);

[Code]....

This is the rest of the code if you need to refer to other codings.

Code:
/**
*@file polygon.c
*@brief Functions for polygon / Struct has polygon numberOfVertices, polygon *xCoordinates and polygon *yCoordinates
*@author: Tan Xian Yao
*@id: 4323440
*@date: 22/04/2014
*/

[Code]...

View 6 Replies View Related

C :: Unable To Read TXT File Into 2D Array

Jan 30, 2013

here is my code, I'm trying to read a .txt file into a 2D array... what's wrong with my code?

Code:

#include <stdio.h>
#define INPUT_FILE_GET_NAMES "names.txt"
#define INPUT_FILE_GET_SALES "sales.txt"
#define MAX_ROWS_NAMES 25
#define MAX_COLS_NAMES 20
#define MAX_ROWS_SALES 25
#define MAX_COLS_SALES 6
//Function Declarations
char getNames(char namesArray[][MAX_COLS_NAMES], char filename[]);

[Code]...

View 1 Replies View Related

C :: Unable To Print Out A Two Dimensional Array

Feb 1, 2013

I have a very simple program here, and somehow I can't get it to print out a two-dimensional array. Here's my code:

Code:

#include <stdio.h>
#define MAX_PEOPLE 100
char display_table(int table[MAX_PEOPLE][MAX_PEOPLE], int n);
int main (void) {
int n = 5;
char names[MAX_PEOPLE][4] = {"Ami", "Bob", "Cal", "Dan", "Ion"};

[Code]...

and here's how the printed table should look like when i run the program:

Code:

|Ami Bob Cal Dan Ion
--- --- --- --- --- ---
Ami| 0 1 1 1 0
Bob| 0 0 1 0 0
Cal| 0 0 0 0 0
Dan| 1 0 1 0 1
Ion| 0 1 1 1 0

View 3 Replies View Related

C++ :: Unable To Input Data From TXT File Into Array Program

Mar 26, 2013

I'm having issues trying to input data from a .txt file into my array program. The whole program takes the name, # of goals and assists, adds them together to get points, and then puts in a given + or - rating. The program works except for the data input.

Also, this is an alphabetical "Unsorted" list, but is there a way to order the list based on point value and be able to display that as well as a separate "Ordered" list?

#include <iostream>
#include <iomanip>
#include <fstream>
#define NUM_PLAY 30
using namespace std;
int buildArrays(int[], int[], int[]);
void printArrays(string[], int[], int[], int[], int);

[Code]...

And this is the .txt file. Order is Players Name, Goals, Assists, Plus/Minus rating. The points are added when points = goals + assists (Should be in the main coding above).

Bryan_Bickell 2 5 +2
Brandon_Bollig 0 0 0
Dave_Bolland 4 2 -1
Sheldon_Brookbank 0 0 -1
Daniel_Carcillo 0 1 +3

[Code]...

View 5 Replies View Related

C++ :: How To Read Data From TXT File Into Array

Feb 25, 2013

I wanna read a txt file like this
1.00518 2.01903 3.01139 4.01343 5.02751 5.99913 7.00011 7.99851 .....
and fill the array P[i][j] with the data.

I try to use

if (myfile.is_open()) {
//for (int i = 0; i < 1; i++)
//{
getline (myfile,DMArray[0]);
cout << DMArray[i] << endl;
//}
myfile.close();
}

But this read the data into a string. How should i do with this string. Besides I know the dimension of the data I am reading.

View 1 Replies View Related

C++ :: Read A File Into 2D Array And Display Data

Nov 15, 2014

I am currently working on a problem where I read a file into a 2D array, and display data. If you saw my code earlier, this is the same problem, but I am now further along.

The file (90 characters) looks like this:
S
R
R
S
R
R
R
R
S
S
....

The "R" resembles rainy days, the "S" resembles sunny days, and the "C" resembles cloudy days. There are 90 characters in the file. The first 30 are for the month of June, the next 30 are for the month of July, and the last 30 are for the month of August. The 31st day of the months are NOT included.

I read the data into a 3 x 30 array, where the row indicates the month (0 = June, 1 = July, 2 = August) and the column indicates the number of days in each month.

Now I need to create a function that creates a report and displays, for each month AND for the whole 3 month period:
1) The number of rainy days
2) The number of cloudy days
3) The number of sunny days
4) Which of the 3 months had the largest number of rainy days

Here is my code so far:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int MONTHS = 3;//Number of months

[Code] .....

There are no compiling errors, but I am not getting the data I want. My rCount and cCount variables end up being 0, and my sCount variable is 90.

View 4 Replies View Related

C++ :: Read From A File And Place Data Into Array

May 3, 2014

Assume you want to read from a file and place the data from the file into an array. The array can hold 10 integers. The file may contain any number of integers with 1 integer per line. Fill in the missing loop test condition so that we will read from the file until either end of file occurs or the array is full. Note we are using cin to read from the file since input redirection is being used.

cin >> temp;
arrayIndex = 0;
while ( _____________________________________) {
array[arrayIndex] = temp;
arrayIndex++;
cin >> temp;
}

View 2 Replies View Related

C++ :: Unable To Open / Read DAT File

Nov 8, 2013

Reading a .dat file, i'm unable to open the file. This program is for a Air Quality index detector, the AQI machine records the particle concentration every minute and saves it into new data file for each day. So I need to make this program run every minute and convert the concentration in to the AQI readings.

The filename is of the format "ES642_2013-11-09.dat", where ES642 is the software name of the machine, and rest is the year, month and day. The code here is just for the file reading section:

Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

[Code] .....

View 4 Replies View Related

C++ :: Sort Data From Text File Into Array As Its Being Read?

Feb 12, 2013

I am suppose to make a program that reads in data from a text file (integers only) and sorts them as it inserts them into an array of size 10. I did this using an insertion sort, which worked great. But now I am being told that I need the function has to read ALL of the numbers in the text file, not just the first 10, and I am not allowed to store them THEN sort, it has to be sorted as being stored.

This is what I have for sorting first 10

void sortArray(int iArray[])
{
string fileName = "";
fstream inFile;
int tmp = 0;

[Code]....

View 3 Replies View Related

C :: Unable To Read TXT File Into A Linked List

Sep 15, 2014

I've been trying to read a .txt into a linked list in the attached code. I'm running into problems, specifically I'm getting errors on line 41 (curr->word=charTemp. I'm trying to set the word array equal to the charTemp array. I've tried strcpy with no luck .

Code:

#include <stdio.h>#include <stdlib.h>
#include <string.h>
//Struct for linked list
typedef struct node {
char word[25];
struct node *next;
} node;

[Code]...

View 2 Replies View Related

Visual C++ :: Unable To Read LCT File To BMP Image

Nov 20, 2014

I have an *.lct file. I like to read *.lct file and save an bmp image.

I can not read this file using usual file read.

Code:

void CLCTtoBMPDlg::OnBnClickedReadlctfile() {
CFileException CFileEx;
CStdioFile ReadFile;
CString OpenlctfilePath;
TCHAR szFilters[]= _T("LCT Files (*.lct)");
CFileDialog fileDlg(TRUE, _T("bmp"), _T("*.lct"), OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters);

[Code]...

Find the attachment. change the file extension *.lct instead of *.txt. I like to read ASCII Data into Hex, Example : Data "A" as "41" (Refer the jpeg image). How is possible?

View 4 Replies View Related

C++ :: Read Data From File Then Make It Into Array Of Structures And Print Afterwards

May 13, 2012

I am trying to read from a data file that has input as :

12.0, 11, 123
14.0, 12.1, 3

And I want the program to read the data from the file and then make it into an array of structures and then print that array afterwards.

Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_INPUT 1000
int n =0;
int i = 0;
typedef struct {
double x[MAX_INPUT];

[Code] .....

The program when run gives the following output:

Ishtiaque-Mohammed-Khans-MacBook-Pro:Comp20005 IshtiaqueMKhan$ gcc -Wall -ansi -o ProjectB ProjectB.c
ProjectB.c: In function "main":
ProjectB.c:59: error: incompatible type for argument 1 of "print_array"

View 1 Replies View Related

C/C++ :: Inputting Data Into Two-dimensional Array

Apr 19, 2014

So I have a two-dimensional array named temperatures[7][2] and I am trying to promt the user to input the highest temperatures for a 7 day period in the first column and the lowest temperatures in the second column. So far all I am getting when I test my code is the temperatures in the second column.

/*Intermediate23.cpp - This program inputs the highest and lowest temps. for seven days. It then displays the average high and low temps. and also diplays the highest temp. that's stored in the first column of the array and displays the lowest temp. stored in the second column of the array.*/

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
//Declare array and variable
int temperatures[7][2] = {0};
int avg = 0;
int totalTemp = 0;

[Code] ....

View 4 Replies View Related

C/C++ :: Unable To Read Data From Buffer Line By Line

Jun 27, 2014

I'm reading strings one after another and trying to split it using strtok_r. This doesnot seem to be working

I have a .gz file from which I'm reading the data into the buffer in chunks.

The below code works fine only for the first chunk on data. Later it just breaks out.

while(1) {
char buffer[SIZE];
        int bytes_read = gzread (f, buffer, SIZE - 1);

[Code] .....

View 3 Replies View Related

Visual C++ :: Unable To Append Data To CFile Using FILE

Sep 26, 2012

I'm trying to append data to a CFile using FILE* But when execute the application, it always give error saying "No such file or directory". I can actually see the file created but it just keep giving error "No such file or directory".

Is the file being lock or the file just created so it can not be find by fstream(FILE*)? or the file mode is wrong?

Below is the code:

[QUOTE]
void SaveDocument(CString strFile) {
CFile file;
if( !file.Open(strFile, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyNone)) {
CArchive ar(&file, CArchive::store);
// save now.
MyClass.Serialise(ar);

[Code]...

Attached is the printscreen of the FILE* pointer. the pointer is evaluated as bad pointer. Why the FILE* pointer not able point to the file being created?

View 7 Replies View Related

C++ :: Unable To Input To Array From A File

Mar 12, 2014

I'm having some problems with Input to an array from a file. I think that i need to make a new array for every instance of the loop but i can't figure out how.

Participator class

void Participator::read(ifstream &stream){
getline(stream, name);
getline(stream, address);
stream >> paid;
}ParticipatorRegister class
void ParticipatorRregister::readFromFile(){

[Code]...

View 1 Replies View Related

C++ :: Concatenate Two 2-dimensional Int Arrays Into One Larger 3-dimensional Array

Jul 31, 2013

How can I concatenate two 2-dimensional int arrays into one larger 3-dimensional array. This question is also valid for the 3-dimensional vectors. I know the command for the one dimensional vector as:

std::vector<int> results;
results.reserve(arr1.size() + arr2.size());
results.insert(results.end(), arr1.begin(), arr1.end());
results.insert(results.end(), arr2.begin(), arr2.end());

and for the one dimensional array as:

int * result = new int[size1 + size2];
copy(arr1, arr1 + size1, result);
copy(arr2, arr2 + size2, result + size1);

But I do not know how to make a 3-dimensional array or vector.

View 3 Replies View Related

C/C++ :: Find Average Of 20 Students By Using A File And One Dimensional Array

Jan 22, 2015

is this according to it??

#include <iostream>
#include <fstream>
using namespace std;
int main(void){
fstream file("textfile.txt");
int linecount = 0;
int tempArray[100];

[Code] ....

View 1 Replies View Related

C :: How To Read Data In A File And Print It Then Copy Contents To A File

Mar 8, 2013

Code:

#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
char buffer[256];
FILE * myfile;
myfile = fopen("read.txt","r");

[Code]...

I also got an error in printing wherein the data in read.txt is "Hello" newline "World" and my program prints world twice.

View 3 Replies View Related

C++ :: Converting One-dimensional To Two-dimensional Array

Jan 17, 2014

I had a hard question in my C++ final exam and I'm trying to solve it for the last 3 days. I haven't succeded yet! Here is the question: You have a one-dimensional array A[20]={1,2,3,4,...,20} and B[5][4] you have to assign the A array's elements to the B array but there is an order which is: B[5][4] = { { 12, 9, 11, 10 }, { 14, 7, 13, 8 }, { 16, 5, 15, 6 }, { 18, 3, 17, 4 }, { 20, 1, 19, 2 } } and there is a restriction: you can only use ONE for statement, nothing else!

#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int A[20] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20 }; // define A array's elements.
int B[5][4] = { 0 }, k = 1; // define B array and k counter.

[code]....

I can't narrow the statements to one,This program works perfectly but it shouldn't be that long, we need ONLY ONE FOR statement, not two!

View 2 Replies View Related

C++ :: Using Loop To Read In Data From A File?

Oct 30, 2013

My function is only reading the first line of numbers even though I have the loop to keep returning to it in main(). How do I have it read from the file until it runs out of data to retrieve?

File being read

90 83 94
72 65 81

File being sent the data(Only the second average is correct, working on the first)

Student Information
Student IDGradeAverageLetter Grade
1895612
90 83 94
289
90 83 94

#include <fstream>
#include <iostream>
using namespace std;
ifstream infile;
ofstream outfile;
void grades(int&,int&,int&); // Grades from the input file
int myavg(int,int,int); // Calculates the Average of the 3 grades

[Code] .....

View 8 Replies View Related

C/C++ :: Read Bytes From A Data File

Dec 5, 2014

I am a member of a group still developing the Microprose European Air War game. We have the source code and permission to do this. As the code dates back to 1998 I use Visual Studio 2006.

I have written many programs in Visual basic, and have the following one to read the airbase value in the "targets.dat" file. The file has a four byte header containing the number of tatgets, and a 32 byte data block containing the data for each target.

Private Sub Command1_Click()
List1.Clear
Dim nt As Long
Dim a4() As Long  
Open App.Path & " argets.dat" For Binary Access Read As 1
'read and display the number of targets  

[Code] ....

Despite many attempts I cannot write the C equivalent of this code into the eaw.exe source. All I really need to know is how to open the 'targets.dat' file and how to get the value of a byte (or integer, or long) at a given position as in the "Get #1, k, a4(n)" line.

View 1 Replies View Related

C# :: Unable To Read Beyond The End Of The Stream Error

Feb 13, 2014

Once it gets to string name = fileWrite.ReadString(); in write file it throws this exception about unable to read beyond the end of the stream.

static void Main(string[] args)
{
WriteFile();
ReadFile();

[Code]....

View 4 Replies View Related

C# :: Unable To Read Each Line From A Listbox

Jun 28, 2014

I started learning programming a few days ago and i'm having trouble with reading each line from a listbox this is the code i'm having trouble with

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string[] getFlies
{
get;
set;

[Code]....

What am I doing wrong?

View 5 Replies View Related

C :: Read Link List Data From File

May 7, 2013

i write this program and every things work correct except when i want to add data of file to the link list .when i add the data to the link list by add_to_linked_list() every things work correct but when i use read_linked_list_from_file() it doesn't work

Code:

#include <stdio.h>
#include <stdlib.h>
// double linked list structure
struct linked_list {
struct linked_list *next;
struct linked_list *prev;
char *value;
};

[code]....

View 2 Replies View Related







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