Help with C I/O Copy Data

Soldato
Joined
9 Dec 2006
Posts
9,287
Location
@ManCave
Code below,
basically if you run it it asks:

Please Enter The Full Path To Your File You Want To Copy To:

perfectly but Carries on running the IF without asking me the Second question. i've tried putting it in else if. but it does not like it either

Please help as im still learning.


PHP:
// Tester.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>   /* required for file operations */
#include <dos.h>  /* for delay */
#include <cstdlib>

//Variables & Pointers

//Vars
char FileNamePath[256] = "test.txt";
char FileNamePathCopy[256] = "test2.txt";
enum {MAX_LEN = 100};

//Pointers
FILE     *OPENFILE, *COPYFILE;            /* declare the file pointer */


//Functions Are Crazy 
void LineReadWrite(FILE *fin, FILE *fout)
{
char buff[MAX_LEN];
while(fgets(buff, MAX_LEN, fin) !=NULL)
    {
        fputs(buff, fout);
        printf("%s",buff);
}
}



int _tmain(int argc, _TCHAR* argv[])
{

    printf("Please Enter The Full Path To Your File You Want To Copy To:\n");
    
    scanf_s("%c",FileNamePathCopy);
    
    printf("Please Enter The Full Path To Your File\n");

    scanf_s("%c",FileNamePath);


    if((COPYFILE = fopen(FileNamePathCopy,"w")) == NULL) //should open file
    
    {
        printf("Cannot Find Your File: %s.\n Please Try Again",FileNamePathCopy); //cannot find file
    }
    
    
    else if

        
        ((OPENFILE=fopen (FileNamePath, "r+"))==NULL)
        {
            printf("Cannot Find Your File: %s.\n Please Try Again",FileNamePath);
        }
    
    
    else
    {
        LineReadWrite(OPENFILE,COPYFILE);
        if(fclose(OPENFILE)==0)
            printf("Closed the %s File Sucessfully\n", FileNamePath);
        if(fclose(COPYFILE)==0)
            printf("Closed the %s File Sucessfully\n", FileNamePathCopy);
    }

    system("pause");
    return 0;
}
 
this not uni/school work

this is just be indepentanly learning c & wanted somthing half fun to work on. (calculator etc is boring)

i fixed the problem. i know have a dfferent problem. i have a function that should search for a keyword in a text file however when i complile
the error "function does not take 0 arguments" (this is search();

could you please tell me why this is?
the rest of the code works fine & displays. (if you remove search(); for compliing

here is the full code
PHP:
// Tester.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string.h>
#include <stdio.h> /* required for file operations */
#include <cstdlib> //Required for System Calls, (System Pause)
//Pointers
FILE * OPENFILE; /* declare the file pointer */
//prototypes
char Search(char SearchText);
//Vars
char FileNamePath[256];
char SearchText[256];
char Text [2000];
char Buffer[512];
//Functions
void ReadTextFile(FILE *READTEXT)
{
 
//fgets( Text, 200, OPENFILE );
//printf( "%s", Text);
while(fgets(Text, 2000, OPENFILE) != NULL)
{
/* get a line, up to 2000 chars from fr. done if NULL */
printf("%s",Text);
/* convert the string to a long int */
}
 
void Search();
//search the text inside the temp.txt 
printf("search");
scanf("%S",SearchText);
while(fgets(Buffer, 512, OPENFILE) != NULL) 
{ 
printf("while msg\n"); 
if((strstr(Buffer, SearchText)) != NULL) 
{ 
 
printf("msg is discarded msg\n"); 
} 
printf("inside of while\n"); 
}
}
int _tmain(int argc, _TCHAR* argv[])
{
printf("Please enter an input filename (use path if needed) :\n");
scanf("%s",FileNamePath);
 
if((OPENFILE = fopen(FileNamePath,"rt")) == NULL) //should open file
 
{
printf("Cannot Find Your Copy File: %s.\n Please Try Again",FileNamePath); //cannot find file
}
 
else
{
printf("Opening %s file successful\n",FileNamePath);
 
ReadTextFile (OPENFILE);
Search();
printf(" Processing here ");
if(fclose(OPENFILE)==0)
printf("Closed the %s File Sucessfully\n", FileNamePath);
 
}
system("pause");
return 0;
}
 
 
From what I can see, you have a few problems with your Search function. The first of these is that your types are mismatched; you have a prototype which declares the function of type char, taking one argument also of type char:

Code:
//prototypes
char Search(char SearchText);

But here where you declare the function, you're trying to declare it as void with no argument.

Code:
void Search();
//search the text inside the temp.txt

This is also terminated by a semi-colon, meaning that it is attempting to declare another function prototype rather than the full function body for the earlier Search of type char. When using function prototypes, the prototype and function declarations must match.
 
Back
Top Bottom