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
ADVERTISEMENT
Nov 5, 2013
I am in an "intro" C++ course and am having trouble with a section of my current project.
We are required to open a text file, read the data from that file, and print it back.
However, we are supposed to let the user input the name of the file that is to be opened. This is the code I have so far. It is not opening anything.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int NUM_QUESTIONS = 30;
typedef char CharArray[NUM_QUESTIONS];
string fileName;
[Code]...
View 2 Replies
View Related
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
View Related
Apr 25, 2013
I have written the following code but i am stuck. Write a program that will prompt the user for a file name and open that file for reading. Print out all the information in the file, numbering each new line of text.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
int main()
{
char line[81], filename[21], c;
int i = 1;
FILE *inFile;
[code]....
View 3 Replies
View Related
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
Mar 14, 2013
Objective of this program is to read binary data from a file and print it.
Code:
#include <stdio.h>
void readFile(void);
int main(){
readFile();
return 0;
[Code] .....
But When I Run This Code First It Print "Error." Then Rest Of The File.Say In My File I Have "I AM HUMAN", It Prints "Error. HUMAN"!!
I Cant Figure Out Whats Wrong In The readFile() Function.It seems right to me.
View 4 Replies
View Related
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
Sep 28, 2014
I'm new to programming and i'm trying to do a certain task. I want to use a for loop to read certain data from a txt file and print them to a console. I'm trying to read student names and their grades.
Something like
3 // 3 represents the number of students.
George 97
Sarah 70
Maya 88
The data may vary but it's always in this format.
View 14 Replies
View Related
Apr 23, 2014
This code works, but not fully This code reads com ports, when a string is available, I check to see if it contains some keywords defined in array If it does, i print out the string itself.
The problem i am having is that it seems to stop processing the data at some point but I do know that there are more strings to be read. So, it does keep reading more lines of data
using System;
using System.Text;
using System.Collections.Generic;
[Code]......
View 9 Replies
View Related
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 10 Replies
View Related
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
Nov 11, 2013
i tring to write a employee payroll code with functions and read data from the txt. file and also bubble sort that sorts and displays starting from employees last name and calculations.
//txt file//
Hours Pay rate empID first name last name
40.0 10.00 A1234 Jane Adams
50.0 10.00 L8765 Mary Lincoln
25.5 10.85 W7654 Martha Washington
52.0 15.75 A9876 John Adams
45.0 25.00 W1235 George Washington
40.25 55.00 L9087 Abraham Lincoln
30.0 9.75 T9876 William Tell
42.5 12.50 M7654 Missy Muffett
30.0 10.00 P8765 Peter Piper
and here is my code
Code:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int SZ = 55;
void tellUser();
[Code] .....
here are the errors the are giving
Code:
payroll.cpp: In function "int bubbleSort()":
payroll.cpp:52:8: error: "lastpos" was not declared in this scope
payroll.cpp:52:18: error: "numemp" was not declared in this scope
payroll.cpp:56:10: error: "swapmade" was not declared in this scope
payroll.cpp:57:16: error: "i" was not declared in this scope
[Code] ....
View 14 Replies
View Related
Dec 26, 2013
I am writing a program where I need to read a byte of char data and convert it into a text string of binary data that represents the hex value...
i.e. The char byte is 0x42 so I need a string that has 01000010 in it. I've written the following subroutine....
------------- My Subroutine ----------------------------------------------------------------------
void charbytetostring(char input, char *output){
int i, remainder;
char BASE=0x2;
int DIGITS=8;
char digitsArray[3] = "01";
[Code] ....
When I submitted the byte 0x42 to the subroutine, the subroutine returned to the output variable 01000010... Life is good.
The next byte that came in was 0x91. When I submit this to the subroutine I get garbage out.
I am using a debugger and stepped through the subroutine a line at a time. When I feed it 0x42 I get what I expect for all variables at all points in the execution.
When I submit 0x91 When the line remainder = input % BASE; gets executed the remainder variable gets set to 0xFFFF (I expected 1). Also, when the next line gets executed..
input = input / BASE; I get C9 where I expected to get 48.
My question is, are there data limits on what can be used with the mod (%) operator? Or am I doing something more fundamentally incorrect?
View 6 Replies
View Related
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
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
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
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
Jan 11, 2015
I am trying to take text from a file and have it displayed using fprintf at line 46. When I run the below code nothing prints out. My text file has 4 lines with 5 different strings separated by whitespace.
#include <cstdlib>
#include <vector>
#include <cstring>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
using namespace std;
//declare my own struct
[Code]...
View 7 Replies
View Related
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
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
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
Mar 28, 2015
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int r = 0;
int c = 0;
int num[17][15] = { 0 };
[Code] ...
// Here is my code for displaying the data from the text file into a 2d array and height next to it, but I am not able to diaplay the height from 60 to 76 next to the row of the 2d array, as shown in the table below. This is my program:
Recently the health authorities have listed a new way to calculate the body mass index (BMI) of an individual.
Values of 20 – 24 are classified as normal, 25-29 as overweight, and 30-34 as heavy.
The table below is a portion of a typical BMI table.
BMI:202122232425262728293031323334
Height:
60102108113118123128133138143149154159164169174
61106111116122127132138143148153159164169175180
.
.
.
MY task is to write a program that does the following things:
1.Produce the table by reading in the data from bmi.txt (on Moodle) into a 2-D array.
2. Display the table neatly on the screen, with row and column headings. The BMI should go from 20 to 34. The height should go from 60 to 76 inches.
3. Prompt the use for their height (in inches) and their weight.
4. Make the program print the closest BMI.
If their weight is lower than the values on the table, use 20 as the BMI.
If their weight is higher than the values on the table, use 34 as the BMI.
5.Loop the program so the user can find more BMI’s.
View 7 Replies
View Related
May 31, 2013
I am trying to get text file and read only first 100 lines of the text content using c/c++. how can i do it?
is it something like string line;
getline(stream,line);
??
View 7 Replies
View Related
Jul 15, 2013
Ok, I think I got the main idea of the I/O chapter. Though I think I might be oversimplifying the program because I am getting an interesting error.
Goal here is to read numbers from a file and print the numbers with totals for two of the columns and a calculated average for of the totals.
NOTE: The tables for the info from file look a lot better on my end. [QUOTE] seems to mess it up a bit from copying and pasting.
Info from problem:
Car No. Miles Driven Gallons Used
----------------------------------------------------
54 250 19
62 525 38
71 123 6
85 1,322 86
97 235 14
carinfo.txt file:
54 250 19
62 525 38
71 123 6
85 1322 86
97 235 14
program:
Code: updated code
View 8 Replies
View Related
Feb 26, 2013
is there anyway to read an entire file and print in on screen? file consists of strings and integers!
View 7 Replies
View Related
Jul 19, 2013
In this program I can not print the typed value in the file. The file creation well done but it is an empty file
I press Ctrl +z for EOF
Code:
#include<stdio.h>
int main(void){
FILE *fp;
char ch;
fp=fopen(" Test.txt","w");
printf(" Enter the TEXT
[Code] .....
View 1 Replies
View Related