I/O Code

Hello, I have created the following program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <stdafx.h>

int main() 
{ FILE * pFile; 
char mystring [100];

pFile = fopen ("encoded.txt" , "r"); 
if (pFile == NULL) perror ("Error opening file"); 
else { 
if ( fgets (mystring , 100 , pFile) != NULL )
puts (mystring); 
fclose (pFile); 
} 
return 0; 


I need to extend the program to incorporate the translate() function and displays the decoded output onto the screen.

Can anyone please help me finish this program and thank you soo much for your help.

1
2
3
4
for (int i=0; i<100; i++)
{
  mystring[i] = translate(mystring[i]);
}
hi thank you for replying but it keeps failing when I try debugging it. Ive tried trouble shooting it in many ways its not working. can you please help me with this.
Your use of indentation scares me.

Assuming you are trying to take a whole text document into a string, I've taken the liberty of making a prototype program that does this for you.
Mind the extra parenthesis at document declaration, as it's needed for proper parsing.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>
#include <fstream>

typedef std::string doc_t;
typedef std::fstream fstream_t;
typedef std::istreambuf_iterator<char> fstream_iterator;

void translate_document( doc_t document_copy);
int main()
{
	doc_t document( (fstream_iterator( fstream_t("document.txt"))), fstream_iterator() );
	return 0;
}
Thanx Nexius, but the code you wrote is doing nothing more then the one I wrote which is only displaying all the integers in the .txt file on the screen.

I need this program to display the decoded output using the char translate (int i) function.
is there no one that can please help me with this...
$ whatis translate
translate: nothing appropriate.

¿What's the problem with Moschops's code?
I keep getting this error when debugging, causing it to fail:

error C3861: 'translate': identifier not found
... that's a compilation error.
¿have you ever defined translate?
no i have never defined translate, how do you do that?
Time to learn about functions:

http://www.cplusplus.com/doc/tutorial/functions/

The key is that if you want to use a function called translate, then that function must have been written by someone.
Im sorry I dont mean to run in circles but I was on the link and I'm still a little confused. is this how you define translate():

1
2
3
4
char translate (int i)
{
return i;
}
Last edited on
no.

1
2
3
4
<type> functionname (<input type> local variable name)
{
    return variable of <type>
}


1
2
3
4
int doubler (int i) // this function has to return an int, and has to be given an int
{
    return i * 2; // return double of int given
}


1
2
3
4
5
int main()
{
    std::cout << doubler(2) << std::endl; // will output 4
    return 0; //0 is success, anything else is failure, main always returns an int, although your compiler will normally add this if you don't
}
Last edited on
¿So what's his mistake? (apart that the function does nothing)
he returned an int instead of a char, the type before the function name, although I'm not sure if that would error considering an int can be represented as a char, still unexpected.
Topic archived. No new replies allowed.