C++ :: Fread Every Nth Character

Oct 16, 2013

I am currently reading several values one after another from a file like so:

Code: ifstream f;
double x;
double a, b, c, d;

f.read((char *)&x, sizeof (double));
a = x;
f.read((char *)&x, sizeof (double));
b = x;

//etc ... for several other values Question:

How can I read every (lets say) 3rd value instead of every next value from the file

View 5 Replies


ADVERTISEMENT

C :: Using Fwrite And Fread On Structs

Jul 20, 2014

I'm currently working on a program that writes an array of struct to a file and then read back the data from the file to another array of struct. At the bottom is an image of my result.

My goal is to end up with two identical struct arrays but my program fails to do this. My struct have to members: ID and kind (of animals in this case). I declare my first arraystruct africa[] with "monkey" and "giraffe" with their respectively IDnr: 112 and 555. I stream this data to a file and read read them back to the arraystruct get_animal[]. Simply I want the get_animal[] to be identical with the africa[] when the program is over, but that is not so. According to my result(bottom image) it display:

112, monkey (get_animal[0])
112, monkey (get_animal[1])
meaning that get_animal[0] is identical to africa[0] get_animal[1] is also identical to africa[0]

but why? I want get_animal[1] to be identical with africa[1]. meaning I want the result to look like this:

112, monkey
555, giraffe

I've also made the program to print the parameters of my fwrite/fread calls. Why is the 3rd parameter = 1 meaning that only 1 element will be read/written when my program just read/write 2 elements?

Code:

main(){
FILE *fp;
struct animals
{
int id;
char kind[20];
} africa[1] = {{112,"monkey"}, {555,"giraffe"}};
}

[code]....

View 3 Replies View Related

C :: Fread / Fwrite And Fseek (reverse Of WAV File)

Jun 28, 2013

I am trying to write a function to reverse a wav file. The idea is to copy the header as it is from the begening of the input.wav file to the beginning of the output.wav file. After that i have to take count number of bytes(count = numberChannels * bitsPerSample in the wav i use this is 2*16= 32 bits, 32/8 = 4 bytes). With this code i am trying to copy the header( that's working fine) and then copy 10 samples from the end and put them to the output.wav file(after header not at the beginning).

This is the content of the input file:

The last 4 bytes of the header are bolded.

Code:
52 49 46 46 24 bd 01 00 57 41 56 45 66 6d 74 20
10 00 00 00 01 00 02 00 44 ac 00 00 10 b1 02 00
04 00 10 00 64 61 74 61 00 bd 01 00 00 10 00 10
ff 00 10 00 00 10 00 10 00 00 ff 10 10 10 00 10
00 00 00 10 10 10 10 00 00 00 10 00 10 00 10 ff
00 10 00 00 10 00 ff 00

This is the content of the output file it suppose to have in my example

Code:
52 49 46 46 24 bd 01 00 57 41 56 45 66 6d 74 20
10 00 00 00 01 00 02 00 44 ac 00 00 10 b1 02 00
04 00 10 00 64 61 74 61 00 bd 01 00 10 00 ff 00
00 10 00 00 10 00 10 ff 00 00 10 00 10 10 10 00
00 00 00 10 10 10 00 10 00 00 ff 10 00 10 00 10
ff 00 10 00

Code:
void reverse(char **array) {
int i=0;
word numberChannels;//word is unsigned short int , byte is unsigned char
word bitsPerSample;

[Code] .....

The problem is that (having in mind per sample is 4 bytes) instead of copying 40 bytes it just copies 20

View 1 Replies View Related

C :: If Fread Returns Char Array Less Than Eight Bytes?

Mar 1, 2013

I do not understand how I can implement this.If fread != to at least 8 bytes then do THIS: printf (" your file is near the end of file", fread result);

View 8 Replies View Related

C :: Crypt File With XOR Instruction - Fread / Fwrite Segmentation Fault

Jan 21, 2015

I try to crypt file with XOR instruction, by always receive segmentation fault. What's wrong with it?

Code:
#include <stdio.h>
main() {
FILE *fin,*fout;
char buff[40];
int a=0x11;
int i=0;

[Code] ....

View 4 Replies View Related

C++ :: Multi-character Character Constant Error Message?

Sep 14, 2014

I keep getting this warning message and I do not know how to fix it. Is it because I'm using char to instead of strings to replace all 't' with 'lp'?

#include<iostream>
#include<string>
#include <stdio.h>
using namespace std;
char * scanf(char * a) {

[code]....

View 6 Replies View Related

C++ :: Reading Data Character By Character From Text File

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

C :: Read From Stdin (File) Character By Character

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

C/C++ :: Read Input File Character By Character?

Aug 10, 2012

How do I write an a program that will read an input file character by character?

View 1 Replies View Related

C++ :: How To Read TXT File Character By Character

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

C/C++ :: Replacing Character In String With Another Character

Sep 13, 2014

So I'm trying to create a function that replaces any instance of a character in a string with another. So first I tried the replace() string member function:

In my implementation file

void NewString::ReplaceChar(const char& target,const char& entry)
{
this->replace(this->begin(),this->end(), target, entry);
};

Main program

#include "NewString.h"
using namespace ...;
int main()

[Code].....

Instead of replacing the the l's with y's it outputted a long string of y's. Also, NewString is derived from the string class (it's for the assignment). the header and whole implementation file, already tested.

I've also tried, instead, to use a for loop in ReplaceChar() but I need to overload the == operator and I don't know how I should exactly:

bool NewString::operator ==(const char& target)const {
if(*this == target)
return true;

[Code]....

I want the == operator to test if the value in the char array is equal to target but I'm not sure how to pass in the position. I'm guessing the this pointer in ReplaceChar() is not the same as the one dereferenced in ==() because target is never replaced by entry in the string.

View 5 Replies View Related

C :: Assignment Of Element Of 2D Character Array To 1D Character Array

Jul 4, 2014

Can we do this :

Code:
char strings[][100]={"ABC","EFG","IJK","LKM"};
char temp[100];
temp=strings[1];

View 3 Replies View Related

C# :: How To Get Int Value Of Each Character In A String And Then Add Them All Together

Aug 20, 2012

I'm trying to get the int value of each character in a string and then add them all together so I can do a 1's complement of the total value. I'm trying to do simple checkum kinda of thing for verification of data.

For example: string DPacket = "Hello World!";

I would like to have each character added and do the ones complement. Will it be easier to convert first to int and then add or any other easier way? So my result should be the decimal value addition of each character and then do the ones complement to that.

View 5 Replies View Related

C++ :: Move A Character Around 2D Map?

Mar 3, 2013

How can I move a character around a 2D map? After some research and a bunch of work I made a function for movement:

unsigned int gamespeed = 100;
unsigned int stage = 1;
void controls()
{

[Code]....

Maps are stored in a different .cpp file

So this code works, but is complicated, ugly and evil (I have to make a pointer to the first map and change the pointer to the next map every time the user reaches the exit, without the pointer this code is, of course, incompatible). How can I reduce this code to be less evil/ugly or at least smaller?

Also it would be nice if the user could move around with arrows as well as with WASD

View 5 Replies View Related

C++ :: Non Printable Character TAB?

Apr 10, 2014

Why TAB with ASCII value 9 is non printable character and SPACE with ASCII value 32 is printable?I can open any editor and press any of them and I see it on screen(If I press tab cursor is moved by tab, and space if I press space)....

Then why one of them is printable and other non printable?

View 5 Replies View Related

C++ :: String Character Removal

Dec 25, 2013

I know how to remove certain characters from a string by using something like this:

Code: string str ("Hello world!");
erase (0, 6);

That's great if I want to do that manually, but say if someone entered a string, how would I automatically remove every other character they entered?

View 4 Replies View Related

C :: Check Certain Character Is In String Or Not?

Mar 6, 2015

I want to check whether a certain character is in a string or not but my code is not working

Code:
#include<stdio.h>
#include<string.h>
int main()
{

[Code].....

View 7 Replies View Related

C :: How To Use Character Pointers Instead Of Arrays

Apr 14, 2013

Here is my program to look up words. How do I use character pointers instead of using arrays?

Code:
#include <stdio.h>
struct entry
{

[Code].....

View 2 Replies View Related

C :: Insert Character To A String

Feb 26, 2013

Let's say i have a string "file.txt" and i want to insert "_out" to make it look like "file_out.txt"

I don't know how to do this ....

View 8 Replies View Related

C :: How To Get Character From A Word With Number

Mar 2, 2013

Any way to do it...its only works with the first letter. So i have a txt file with information such as de3 dn5 dn7 dw9 ds1 and how to get the letters and then the number.

example:

if de print this is de; and print the number that follow de

Code:

#include <stdlib.h>
int main ()
{
FILE * pFile;
int c;
pFile=fopen ("de3.txt","r");
if (pFile==NULL) perror ("Error opening file");
else

[Code]...

View 5 Replies View Related

C :: Reading In Character In Program

Sep 10, 2014

I just took up c programming and like it so far. Now I got a problem reading in a character in a program. The program looks like this:

insert
Code:

#include <stdio.h>
main()
{
char c1, c2;
printf("Letter 1: ");
c1 = getchar();

[Code]....

View 8 Replies View Related

C :: Check Whether Input Is A Character Or Not

Apr 27, 2013

Code:
char value;
printf_s("enter:");
if (scanf_s("%c", &value) != 1)
{
printf_s("oppppssss
");
}
else
{
printf_s("ok");
}

I wanted to check whether the input is a character or not, if a character is given then the output suppose to be "ok", but the output is always "oppppssss", where is the problem here?

View 1 Replies View Related

C :: How To Input Inventory For Character

Jan 15, 2015

im working on mine first 2d game and im practicing a lot, how to input my inventory for character ( for items, and others ) So, i just want to let u know that i have :

#define WEAPON_LONGSWORD (1)
#define WEAPON_WARAXE (2)
#define WEAPON_DAGGER (3)
#define WEAPON_SHORTSWORD (4)
#define WEAPON_BOW (5)
#define WEAPON_MAX (6)

So, i want to put this in my shop and if i buy it i want to appear in my inventory, or sell sell trought my inventory.Also i have one define

#define WEAPONBEGIN_WOODENSTICK (1)

Which will go instant ( when u create character ) to inventory. And:If i have 1, 2, 3, 4, 5, 6, 7, 8, 9 slots ( example ) and i sell item which was on 3rd slot, what will happen with others ? will they just go back for one slot or ?

View 5 Replies View Related

C :: Copy A Character To String

Mar 6, 2015

Copy some characters from char * arg to char * first using a loop with specific conditions.

Code:

char * arg;
// set arg some string...
char first_[25];
char * first;
int length;
length=strlen(arg);
for (n++; arg[n] != '}' || n>=length-1; n++)
strcpy(first,arg[n]); // first += arg[n]; I have strcpy(first,arg[n]); but arg[n] is char and strcpy expects char * ;

how to solve this?

View 2 Replies View Related

C :: How To Move A Character In 2D Array

Jan 27, 2015

I want to move a character in a 2D array This Character should move vertically in a 2D Array To exemplify it for Exam in Snake Game A character automatically moves Please Write a example code that works

View 4 Replies View Related

C :: Display One Character In A String?

Jun 14, 2013

It's been about two years since I last program c, now I need to do it for a basic project. How would I print out one letter in a string?

For example lets say I have a string called str=[Hello]. I want to display the third letter so just the "l". Here's what I have so far:

Code:

#include<stdio.h>
int main() {
char str[50] = "Hello";
printf("The third letter is : %s
",str[3]);
return 0;
}

View 2 Replies View Related







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