C :: Printing Contents Of A File - Prints One Extra Character Not Present In The File
Feb 12, 2013
I'm writing a program that stores records into a file and then these records can be printed out. A last name, first name, and score is stored to be exactly 36 characters long (using leading spaces to pad) making it easier to retrieve specific records. For example, three records stored in the file would like like this: (the underscores are simply to illustrate the distance, they are not in the file itself)
_______lastname_______firstname__90__________lname __________fname_100___________last___________first __60
When printed out, the names are formatted as follows:
lastname, firstname: 90
lname, fname: 100
last, first: 60
However, when I print them out this is what I get:
lastname, firstname: 90
lname, fname: 100$
last, first: 60H
For some reason, for any record after the first, an extra character is added to the end. These characters are not in the file, so I was thinking that the array for some reason wasn't being filled completely, (the array is initialized to size 36 and 36 characters are read from the file using fread) so it was printing out a random character assigned to the 36th array position. Except the character never changes, (always a $ for record 2, H for record 3, l for record 4 if i remember) and I've tried reducing the array size or the number of character read and it's the string that gets altered, the random character always remains. I figure the problem must be in the print_records function (appending seems to work no problem). Anyway here is my print records and appending records code.
Code: /*
- Prints a single record stored in the file pointed to by ifp.
*/
void print_record(FILE *ifp, int record) {
[Code]......
View 6 Replies
ADVERTISEMENT
Jan 2, 2015
I am trying to establish a connection between client and server to send a file to the server from a client. I have been successfully able to send files to the server but i am facing a problem with the the filename whenever i try to send any string to the server and use it in naming the filename at the server side, the string is successfully concatenated but it saves the filecontents in the filename.
for example: i am sending a file hello1.txt to server and the server has to save it as abcxyz.txt as i am sending the "xyz" from the client. BUT Whenever i am doing this ,the file saves as abcxyzfilecontents.txt If i saved in the .txt file "you123" ,my file at server side would save as abcxyzyou123.txt
Here are my codes:
Know that the server code implements a multi threaded server. The functionality to be discussed is defined in myfunc
Code:
server.c:
#include <stdlib.h>
#include <stdio.h>
[Code].....
View 1 Replies
View Related
Jan 2, 2014
I am trying to store a large text file into an array so I can count the number of characters.
View 2 Replies
View Related
Oct 15, 2012
there is a file contains only a numbers ,we dont know how many numbers present in that file.so i want a program to display top n largest number present in that fie.(n may be 5,10,12 like that.)
View 3 Replies
View Related
Apr 25, 2014
I am reading a file whick looks like following:
Code:
10 = NAND(1, 3)
11 = NAND(3, 6, 5)
15 = NAND(9, 6, 2, 8)
Problem: I have to find the word "NAND" and then find the numbers inside the brackets because they are the inputs to that NAND gate. I have written a code below but that code can detect the fixed number of inputs. I need a code which can detect any number of inputs (whether 2 inputs or more than two). But i don't understand how do i do that?
My code:
Code:
string input_str ("INPUT"), output_str ("OUTPUT"), nand_str("NAND");
while (getline( input_file, line ))
{
std::size_t guard_found = line.find(guard_str);
[Code].....
View 8 Replies
View Related
Oct 28, 2014
I am trying to write my files to another subfolder . Below is my program.
Code:
cat th6.c
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#define SIZE 8
#define NUMELM 8
[Code] ....
I observe my filename along with directory as text in the new file created in sublfolder. This is output.
[cporgs/apue/threads]: mkdir first
[cporgs/apue/threads]: ./a.out test.txt first
test.txt -- first/test.txt
dfdfdfdfdfdfdf-14
dfdfdfdfdfdfdf-14
in thread test.txt first
[cporgs/apue/threads]: cat first/test.txt
dfdfdfdfdfdfdffirst/test.txt
[cporgs/apue/threads]: cat test.txt
dfdfdfdfdfdfdf
I could not able to get from where this filename and folder is getting added.
View 4 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
May 17, 2013
Saw a thread about a program to add an extra 'b' before 'B' in a file containing 'ABC'. I tried it. Here's my code:
("file1.txt" contains "ABC")
#include<iostream>
#include<fstream>
using namespace std;
int main(){
ifstream in;
ofstream out;
[Code] ...
It works except an extra C appears at the end.
O/P in the file:
"AbBCC"
View 4 Replies
View Related
Nov 15, 2013
My file has these lines.I try to remove extra line in the end, in notepad/gedit, however, after saving it automatically adds up there.
one
two
three
Code:
void help::read_ref(const char* file_name){
std::ifstream file_handle;
file_handle.open(file_name);
std::string line,name;
getline(file_handle,line);
while(!file_handle.eof()){
[code]......
when I print size of map, it comes out to be an extra then what is should be.
std::map<std::String,std::string> ->is my map
on the contrary if I use Code: file_handle >> line; I get correct number of entries in map.
My doubt: getline, automatically finds and in last, it finds a , why it doesn't end the iteration?
I have faced this issue many number of times.
Code: line[0]=='>' I am making this check, then also, I get a null string as key in my map.
I have printed key with their index, using Code: map iterator .
View 7 Replies
View Related
Mar 17, 2013
Right now I'm working on an assignment where I need to take the data from on file encrypt it by adding 5 to each byte and then save it to a user specified location. I think I'm close to having it done but I've run into a hick up. After I get the user input for where the encrypted data should be saved the program seems to never end. This is the code I have so far
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream input;
ofstream output;
[Code]...
View 1 Replies
View Related
Jan 12, 2014
private void open(object sender, EventArgs e) {
OpenFileDialog openDialog = new OpenFileDialog();
if (openDialog.ShowDialog() == DialogResult.OK) {
string[] lines = File.ReadAllLines(openDialog.FileName);
int k = 0;
while (k < lines.Length)
[Code] ....
So in this code, the user basically opens a text file from the dialog. Then the file gets read on all the lines and gets stored in an array. Now the reason I did this is because there are more fields of text boxes I have to fill in after I fill in the table (6x3). I'm getting an out of bounds error however.
19
19
19
0
95
0
0
0
0
5
0
0
5
1
0
0
51
1
110
Warrior
This is a sample text file that is being used. As you can see all the lines from start up until (excluding "110") will be used in the table. Now I want to read the last lines in this text file and put it in another text box. How do I do that?
View 2 Replies
View Related
Feb 9, 2012
Why this code outputs 7 lines (the last line twice) while the file contains 6 lines?
cout << "read msgfile
";
ifstream msgfile ("script1.msg");
while (msgfile.good()) //if not at end of file, continue reading {
// load vector with deffile
msgfile >> line;
vectormsgfile.push_back (line);
cout << line << "
";
}
msgfile.close();
View 2 Replies
View Related
May 21, 2012
When I read a text file. I'm reading a list of strings in text file, with one string per line. The first line has extra characters in the string, the rest of the lines read are fine, and I can't understand where the extra characters come from. The file format is this...
A
AA
AAN
AAP
AAPL
AAWW
AAXJ
....
...
Code:
std::vector<std::string> strSymbolList;
std::string s = "";
std::ifstream infile( m_strFileSymbols );
while( std::getline(infile, s) ) {
strSymbolList.push_back(s);
}
infile.close();
View 3 Replies
View Related
Aug 18, 2013
I'm working on a cross-platform threading project, the aim is to start 1 worker-thread that has a queue of worker-items which are serviced by the thread.
The threading framework works fine, both under Windows and LINUX, albeit as designed under Windows, and in a modified form under LINUX.
I have this CSnmpTrapd class which is designed as a worker-item for the worker-thread, it listens for incoming SNMP traps and decodes the packets, this is implemented in csnmptrapd.cpp and csnmptrapd.hpp.
1) When I add this piece of code to my makefile and re-compile the app successful... the app will start (or not?) but generates no console or file logging as is expected, it will not even show printf() output which was added as first statement in main(). My app handles CTRL-C to terminate and this works !?!? But I'm actually not sure my app works at that point.
2) When I compile the app without csnmptrapd.cpp and start it, it will generate console and logging output and behave as expected.
3) When I add the CSnmpTrapd class implementation to an existing cpp file in the project, it will compile and run as expected... the worker-thread handles the CSnmpTrapd worker-item as it should, incoming SNMP traps are captured and decoded, logging generated
View 6 Replies
View Related
Feb 18, 2014
I am writing a code with C where I will give an integer and get the binary conversion of it.I tried to fill the binary digits into an integer array.But when I normally print it will give the proper output.But when I try to print the contents of the array it will not produce the proper result.
My code would be as follows.
#include <stdio.h>
#include <conio.h>
int main()
[Code]....
View 5 Replies
View Related
Jul 25, 2012
Double values are stored in text file. 23.5 36.8 34.2 ... My teacher told me to read them character by character and then make words, like i have to read "2" "3" "." "5" and now have to make it or treat it as word and then using atoi(). I have to convert it into double. but i dont know how to do this....
View 5 Replies
View Related
Nov 24, 2013
I have this code for a computer project... (store management) but the character strings are not copied on text file..
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class Store {
public:
char *item_name[5];
store()
[Code] .....
Now when i run the program, it gives a error :::
ERROR
address 0x0
How can i write these strings to the text file?
View 2 Replies
View Related
Nov 10, 2013
I have to optimize a code for below scenario. I am reading stdin (a file redirected to stdin) character by character. How many chars are going to come is not known. After every few chars there is a seaparator. e.g $ as below
rhhrkkj$hghjhdf$ddfkrjt
While reading, if the separator arrives I'm processing the string stored before that separator and then continue reading stdin in same fashion, till EOF. I am using getc(stdin) to read chars.
Using gprof I can see most of the program time is spent inside main() , for this reading logic. Rest of the program is just some insert and search operations. I am getting time of 0.01 secs at the moment, want to reduce further.
View 6 Replies
View Related
Aug 10, 2012
How do I write an a program that will read an input file character by character?
View 1 Replies
View Related
Jan 27, 2014
For the last part of this problem, if player 2 loses the game of hangman, I need to display the letters they did get right. In order to do that, I believe that I need to traverse vector v for the elements that exist in the string hiddenword, and then print those characters that match.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int letterFill (char, string, string&);
[Code] ....
View 1 Replies
View Related
Nov 7, 2014
I am reading a file then printing the data onto the other file. It is working, however when I check to see if each variable is being properly set after reading the file a issue arises.
Example of the file being read
Code:
Vehicle PV50CAN passed camera 1 at 05:33:26.
Vehicle W867BRO passed camera 1 at 05:33:29.
Vehicle KQ63ARU passed camera 1 at 05:33:38.
Vehicle K954ITQ passed camera 1 at 05:33:40.
Vehicle V220MXB passed camera 1 at 05:33:42.
[Code] .....
View 14 Replies
View Related
Feb 1, 2014
In the function *expandArrayList and *trimArrayList I need to allocate either more memory or less memory to the given array. Right now I am using malloc on a new array and then transferring the elements of the original array to the new array, freeing the original array, and then reassigning it to the new array so it's size and capacity are either bigger or smaller than the original. I feel like there has to be a simpler way to do this with either realloc or calloc, but I cant find sufficient information on either online. Would I be able to use either to make this easier and the code cleaner?
Also, these are character arrays that contain several character arrays inside them, so I was wondering what the best way to transfer the contents of one array to the other would be. I have gone between
Code:
//example
for (i=0; i<length; i++){
newarray[i] = oldarray[i];
}
and
Code: for (i=0; i<length; i++){
strcpy(newarray[i], oldarray[i]);
}
but I'm not sure which one (if either) should work.
The 'ArrayList.h' file that is included contains the structure ArrayList, as well as the function prototypes for the functions listed below.
Here is the structure in ArrayList.h:
Code:
#define DEFAULT_INIT_LEN 10
typedef struct ArrayList {
// We will store an array of strings (i.e., an array of char arrays)
char **array;
[Code] ....
Here is my code:
Code:
//included libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ArrayList.h"
//Create arraylist
[Code]....
View 3 Replies
View Related
Oct 6, 2013
In my program, I'm supposed to read a text file (the name of which is given to me as a command line paramater, as long with an integer), and display the text in a specific format (each line can only be as long as the integer). However, I'm having trouble even reading the text file. I don't know the syntax. I'm only allowed to edit the function that does the formatting, and the code in that is
void typeset (int maxWidth, istream& documentIn)
I don't know how to 'read' the file, as most examples online are ifstream, or openFile or something like that. What I want to do is just read the first character of the file, and continuously keep reading characters until the end of the file.
View 10 Replies
View Related
Jan 8, 2014
How can I read contents of a file from the last character? Can I use a loop?
View 10 Replies
View Related
Jan 30, 2015
I need a sample program to read contents of any xml file using CPP.
View 2 Replies
View Related
Dec 30, 2013
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<fstream.h>
#include<graphics.h>
void input();
void output ();
[Code] ....
View 1 Replies
View Related