C++ :: Open A Text File Then Read Data And Print It Back?

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


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 :: Create File To Write Text And Read Back The File Content

Mar 15, 2013

The Objective Of This Program Is To Create A File To Write Text And Read Back The File Content. To Do That I Have Made Two Function writeFile() To Write And readFile() To Read.The readFile() function works just fine but writeFile() doesn't.

How writeFile() function Works? when writeFile() function Execute It Takes Characters User Type And When Hit Enter(ASC|| 10) It Ask "More?(Y/N)" That Means What User Want? Want To Go Next Line Or End Input?

If "Y" Than Inputs Are Taken From Next Line Else Input Ends.

But The Problem Is When Program Encounters ch==10 It Shows "More?(Y/N)" And Takes Input In cmd variable.If cmd=='Y' I Mean More From Next Line Than It Should Execute Scanf Again To Take ch I Mean User Input.But Its Not!!! Its Always Showing "More?(Y/N)" Again And Again Like A Loop.

Code:
#include <stdio.h>
void writeFile(void);
void readFile(void);
int main(){

[Code].....

View 5 Replies View Related

C :: Read Txt File And Print Contents Numbering Each New Line Of Text?

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

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 :: Program To Read Binary Data And Print It - File I/O Error

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

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

Visual C++ :: Use For Loop To Read Certain Data From Txt File And Print To Console

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

C++ :: Not Able To Read Data From Text File And Convert It To Integer

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

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

Visual C++ :: Employee Payroll - Read Data From Text File And Sort

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

C++ :: How To Save Struct To File And Then Read It Back From The File

Aug 5, 2013

How to save the struct to file and then read it back from the file ?

Code:
#include <conio.h>
#include <stdio.h>
struct student {
int person;
int egn;
float AvergeGrade;

[Code] ....

Have average grade: %f", person[i].FirstName, person[i].LastName, person[i].egn, person[i].AvergeGrade);
}
//Save to file
getch ();
}

View 2 Replies View Related

C :: Writing Structure Array Bytewise To Binary File And Read It Back

Aug 21, 2014

I would like to write a complete structure array to a file and read it back, recovering all the data. I have tried the following:

Code:

#include <stdio.h>
#include <string.h>
#define NUM 256
const char *fname="binary.bin";
typedef struct foo_s {
int intA;
int intB;
char string[20];

[Code]...

//---------------------------------------------------- but the mac field is reading back some random value repeatedly. Why is that? And how do I fix this?

View 8 Replies View Related

C++ :: Read Byte Of Char Data And Convert It Into Text String Of Binary Data That Represents Hex Value

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

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 :: Cannot Open A Text File

Apr 21, 2013

I'm trying to type out whatever is in the .txt document, but I cannot open it for some reason. My code is

Code:
int main(int argc, char *argv[]) {
char ch=0;FILE *fp;
fp=fopen("C:UsersPCDesktopNew folderQuestion 5one.txt",'r');
while(ch!=EOF){
printf ("%c", ch);
ch=getc(fp);
Sleep (200);}fclose(fp);
return 0;
}

I keep getting an error with my fp=fopen so how do you open the text? Do i have anything else wrong with my code.

View 9 Replies View Related

C++ :: Allow User To Open File And Either Read Or Write To It

Jan 30, 2014

So I've been turning my programs into classes and I run into errors.

So this program is supposed to allow the user to open the file and either read or write to it.

I've omitted the read part from it as I want to attempt that on my own .

I get these compile errors:

fileclass.cpp:13: error: ISO C++ forbids declaration of ‘choice’ with no type
fileclass.cpp:21: error: ISO C++ forbids declaration of ‘choice’ with no type
fileclass.cpp: In member function ‘int file::choice()’:

[Code] .....

View 10 Replies View Related

C++ :: Absolute Path Reference - Open File And Print Contents To Terminal Window

May 12, 2014

I am trying to open a file and print the contents of the file to the terminal window. It works when I put the file right in the directory with the Solution but not if the file is out on my desktop and I use the full path. Here is the code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int OpenFile(){
fstream SampleFile;

[Code] .....

View 5 Replies View Related

C++ :: Open And Store A Value To Text File

Jan 29, 2014

This my deposit function, i want to open the file (balance), then add the sum and store in on the text file.

float deposit( int x, int currentBalance) {
cout<<"
"<<endl;
cout<<"Welcome to the deposit area"<<endl;
cout << "Your new balance is:" << balance <<endl;

[Code] ....

View 13 Replies View Related

C++ :: Printing From Open Text File?

Nov 14, 2013

I need to create a text file that looks like:

first line
second line
third line
forth line
fifth line
sixth line

I want to replace the third and forth lines with three new lines. The above contents would become:

first line
second line
new line1
new line2
new line3
fifth line
sixth line

How can I do this using c++?

never was taught file function in c++

View 3 Replies View Related

C :: Open Up File / Read Until EOF And Report Count And Sum Of All Numbers?

Mar 10, 2013

I'm having trouble with how to find the count and sum of all numbers.

The file, called question1.txt, contains numbers:

1
2
3
4
5
0
0
0
-2
-2

Here's my code:

Code:

#include <stdio.h>
#include <stdlib.h>
int main (void)
{
FILE* spIn;
FILE* spOut; //for output file
int numIn;

[Code]...

View 1 Replies View Related

C/C++ :: How To Create A Txt File - Read / Write And Open For More Information

Jan 25, 2015

How do i create a text file read and write in it but every time i reopen the program i must not override the data in the text file .

View 6 Replies View Related

C/C++ :: How To Open Text File From A Specific Location

Feb 2, 2014

I'm having trouble opening a file from a specific location.

#include "maidcafe.h"
....
char directory[80] = {"C:UsersPublicDocumentsAnime NotesTransaction"};
char code[20];
printf("Enter file name");
gets(code);
strcat(directory,code);
system("start "" %s",directory);    

i thought this would work, but it didn't xD. I got an error

View 3 Replies View Related

C++ :: Open Text File From Embedded Resource?

Apr 19, 2014

I have the following complete code in c++

Code:
void GetMapData () {
MapPair NewMap;
string TempString;
BOOL SkipReadLine = FALSE;
//------
char buffer[MAX_PATH];
std::fstream File;

[code].....

The problem is that I do not want to have an external MapData.txt, rather I access it through a embedded text resource, which in this case I have no knowledge on doing so.

I have the following snippet which demonstrates to my knowledge on how to access a text resource; however, I do not know hot to modify the code above to implant this, but to still have the same functionality as the original code above.

Code:
HRSRC hRes = FindResource(GetModuleHandle(NULL), MAKEINTRESOURCE(IDR_TEXT1), _T("TEXT"));
DWORD dwSize = SizeofResource(GetModuleHandle(NULL), hRes);
HGLOBAL hGlob = LoadResource(GetModuleHandle(NULL), hRes);
const BYTE* pData = reinterpret_cast<const BYTE*>(::LockResource(hGlob));

note that I am compiling with a MBCS.

View 2 Replies View Related

C :: Open New Text File With First Letter Of Line On A Certain Directory

May 23, 2013

I'm reading from stdin a line. With that line, I should open a new textfile with the first letter of that line on a certain directory. My code is the following :

Code:

int main() {
char line[BUFSIZ];
FILE *ptr_file;
int x;
while(fgets(line,BUFSIZ,stdin) != NULL){

[Code] ....

char caminho[] is the directory in which I want to create the text file and chave will be the first letter of the line in stdin.

How am I supposed to use strcat to get these two together in a string to then use ptr_file =fopen(caminho, "w");

View 3 Replies View Related

C# :: Open And Read CSV File One Line At A Time Using SQL Commands - OleDb Exception

Apr 7, 2010

The objective: Open and read a CSV file one line at a time using SQL commands.

The problem: I am getting an error that I have a feeling may not point to the "real" problem.

Where I may have syntax or other errors in code.

stInputConn = "Provider=Microsoft.jet.OLEDB.4.0;Data Source=V:\IT\RE\Exports\;Extended Properties="text;HDR=YES;Format=Delimited(,)"";
OleDbConnection cn = new OleDbConnection(stInputConn);
stInputFileName = ConfigurationManager.AppSettings.Get("InputFile");
// the input file name is CSV_DataExport.CSV

[Code] .....

The last line (ExecuteReader) is where I get the OleDb Exception.

View 9 Replies View Related







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