473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

The function readit() seems to jump to writeit() when it shouldn't

10
::links not allowed, please read the Posting Guidelines::

The function readit() seems to jump to writeit() when it shouldn't in read.c This has got me stumped. After inside the readit() gets called it seems to when the function ends it seems to just process the writeit() when it isn't being called. Any help would be much appreciated :P
Sep 4 '07 #1
10 1752
doskey
10
I know the checkbuff() function is lame that's why it isn't being used.
Sep 4 '07 #2
sicarie
4,677 Expert Mod 4TB
I know the checkbuff() function is lame that's why it isn't being used.
Can we see a snippet of your code? The useful parts would probably be the declarations and the implementations of those functions, maybe a line or two before and after.
Sep 4 '07 #3
doskey
10
Well i will post 2 pages
It seems to be when the readit() gets called, it just jumps and writeit() gets processed

-------READ.C----------------
Expand|Select|Wrap|Line Numbers
  1. #ifndef filesize
  2. #define filesize 2001
  3. #endif
  4.  
  5. struct tmpcommands{
  6.        FILE *fp;
  7.        };
  8.  
  9. extern void filehelp_text(char file[]){
  10.        printf("\nFILE FUNCTIONS: ");
  11.        printf("\nUsage is %s <COMMAND> <FILE> -f \n", file);
  12.        printf("Commands are -read, -append, -write, -delete\n");
  13.        printf("Remember the -f flag must be activated to use the file functions\n");
  14.        }
  15.  
  16.  
  17.  
  18. extern void readit(char file[]){
  19.        char filedata[80];
  20.        struct tmpcommands init;
  21.        init.fp = fopen(file, "r");
  22.        while(fgets(filedata, 80, init.fp)!=NULL)
  23.        printf("%s\n", filedata);
  24.        fclose(init.fp);
  25.        }
  26.  
  27.  
  28. extern void writeit(char file[]){
  29.       char filedata[80];
  30.       struct tmpcommands init;      
  31.  
  32.       if(init.fp = fopen(file, "r"))
  33.          if(init.fp != NULL){
  34.                int hasbofprotect = 0;
  35.                char writedata[filesize];
  36.                char bofprotect[filesize];
  37.                fprintf(stdout, "The file contains data,\nplease enter some data to create the file and save the text\n");
  38.                fclose(init.fp);
  39.                init.fp = fopen(file, "w");
  40.                fscanf(stdin, "%s", &writedata);
  41.  
  42.                if(strlen(writedata) >= filesize){
  43.                        fprintf(stdout, "Error max file size length is 2000 bytes\n you entered %d bytes\n", strlen(writedata));
  44.                        fprintf(stdout, "Please enter some text, remember max file size is 2000 bytes. :P\n");
  45.                        fscanf(stdin, "%s", &bofprotect);
  46.                        fprintf(init.fp, "%s", bofprotect);
  47.                        hasbofprotect = 1;
  48.                        }
  49.  
  50.                if(hasbofprotect == 0){
  51.                fprintf(init.fp, "%s", writedata);
  52.                fprintf(stdout, "Everything is wrote to the file.\n");
  53.                fclose(init.fp);
  54.                }
  55.  
  56.          }
  57.          else
  58.          {
  59.                char writedata[filesize];
  60.  
  61.  
  62.  
  63.       fprintf(stdout, "You have chosen to write some data into the file: %s.\n", file);
  64.  
  65.  
  66.       }
  67.       }
  68.  
  69. extern void appit(char file[]){
  70.       char filedata[80];
  71.       struct tmpcommands init;
  72.      }
  73.  
  74.  
  75.  
  76. #ifdef filesize
  77. #undef filesize
  78. #endif
----MAIN.C-----------

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "main.h"
  5. #include "safe.c"
  6. #include "read.c"
  7.  
  8. /* checkbuff(buffer); A little command i made to check for bof in text which can be quite usefull */
  9.  
  10. static void command(char command[commandlen], char file[20], char flag[2]){
  11.        struct tmpcommands issue;
  12.        char com[10];
  13.        int iffile = 0;
  14.  
  15.        if(strcmp(flag, "-f") != 1)
  16.          iffile = 1;
  17.  
  18.        if(iffile != 0){
  19.        if(strcmp(command, "-read") != 1)
  20.        readit(file);
  21.  
  22.        if(strcmp(command, "-write") != 1)
  23.        writeit(file);
  24.  
  25.        if(strcmp(command, "-append") !=1)
  26.        appit(file);
  27.  
  28.        }
  29.        }
  30.  
  31.  
  32. int main(int argc, char *argv[]){
  33.  
  34. /* Main variables */
  35. char buffer[max_buff];
  36. char proccommand[commandlen];
  37. int i=0;
  38.  
  39. if (argc < 4 ){
  40.  
  41. filehelp_text(argv[0]);
  42. fprintf(stdout, "I am busy adding more commands, this program is still under development\n\n"); 
  43. return 1;
  44.  
  45. }
  46.  
  47. command(argv[1], argv[2], argv[3]);
  48.  
  49.  
  50. return 0;
  51. }
Sep 5 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
if(strcmp(command, "-read") != 1)
readit(file);

if(strcmp(command, "-write") != 1)
writeit(file);

if(strcmp(command, "-append") !=1)
appit(file);
This is not good programming style. strcmp() returns a value of 0 when the comparison is equal and a value <0 when command is less than the second srgumennt. Both of these are !=1.

So if command is "-append", that's less than "-read", you you call readit().
Then "-append" is less than "-write" so you call writeit().
Then "-append" is equal to "-append" so you call appit().

Do you mean:

Expand|Select|Wrap|Line Numbers
  1. if(strcmp(command, "-read") != 0)
  2. readit(file);
  3.  
  4. if(strcmp(command, "-write") != 0)
  5. writeit(file);
  6.  
  7. if(strcmp(command, "-append") !=0)
  8. appit(file);
  9.  
???
Sep 5 '07 #5
doskey
10
This is not good programming style. strcmp() returns a value of 0 when the comparison is equal and a value <0 when command is less than the second srgumennt. Both of these are !=1.

So if command is "-append", that's less than "-read", you you call readit().
Then "-append" is less than "-write" so you call writeit().
Then "-append" is equal to "-append" so you call appit().

Do you mean:

Expand|Select|Wrap|Line Numbers
  1. if(strcmp(command, "-read") != 0)
  2. readit(file);
  3.  
  4. if(strcmp(command, "-write") != 0)
  5. writeit(file);
  6.  
  7. if(strcmp(command, "-append") !=0)
  8. appit(file);
  9.  
???

Well..... I thought strcmp returned 0 if the comparison is equal. I have changed it to !=0 and this is the result

doskey@DosKeys-Xion-sys:~/Desktop/project/src$ gcc main.c -o test
doskey@DosKeys-Xion-sys:~/Desktop/project/src$ ./test -read c -f
doskey@DosKeys-Xion-sys:~/Desktop/project/src$ ./test -read c -fa
The file contains data,
please enter some data to create the file and save the text
kjh
Everything is wrote to the file.

For some reason the -f flag needs another byte.... actually you can use any two bytes like -ba as an arguement. Also as you can see, when i used -read it still skipped and went to the writeit(). No matter what i put -append, -read, -write it's always jumping to the writeit() function.

This is the modified command() function

Expand|Select|Wrap|Line Numbers
  1. static void command(char command[commandlen], char file[20], char flag[2]){
  2.        struct tmpcommands issue;
  3.        char com[10];
  4.        int iffile = 0;
  5.  
  6.        if(strcmp(flag, "-f"))
  7.          iffile = 1;
  8.  
  9.        if(iffile == 1){
  10.        if(strcmp(command, "-read")!=0)
  11.        readit(file);
  12.  
  13.        if(strcmp(command, "-write")!=0)
  14.        writeit(file);
  15.  
  16.        if(strcmp(command, "-append")!=0)
  17.        appit(file);
  18.  
  19.        }
  20.        }
  21.  
As you can see this program is doing stuff it hasn't been programmed to do.
I NEED HELP !!!!!!!!
Sep 6 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
How about you try:

Expand|Select|Wrap|Line Numbers
  1. if(strcmp(command, "-read") == 0)
  2. readit(file);
  3.  
  4. if(strcmp(command, "-write") == 0)
  5. writeit(file);
  6.  
  7. if(strcmp(command, "-append") ==0)
  8. appit(file);
  9.  
My apology that I didn't see this on my previous post.
Sep 8 '07 #7
doskey
10
It seems it doesn't work. I don't know where i am going wrong here? HELP !!!!
Oct 5 '07 #8
weaknessforcats
9,208 Expert Mod 8TB
Your problem is somewhere else. I took this code and made some stub functions and hard-coded a command. I then changed the command, recompiled. Each time I got the correct results: only one function was called.
Expand|Select|Wrap|Line Numbers
  1. void readit()
  2. {
  3.    cout << "readit" << endl;
  4. }
  5. void writeit()
  6. {
  7.    cout << "writeit" << endl;
  8. }
  9. void appit()
  10. {
  11.    cout << "appit" << endl;
  12. }
  13. int main()
  14. {
  15.       char command[] = "-append";
  16.       if(strcmp(command, "-read") == 0)
  17.         readit();
  18.  
  19.         if(strcmp(command, "-write") == 0)
  20.         writeit();
  21.  
  22.         if(strcmp(command, "-append") ==0)
  23.         appit();
  24.  
  25. }
  26.  
Oct 5 '07 #9
doskey
10
That's C++, you can't use C++ in your C code :S
Oct 11 '07 #10
weaknessforcats
9,208 Expert Mod 8TB
That's C++, you can't use C++ in your C code :S
You are right. Old habits. Just change the cout to a printf.
Oct 11 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Margaret MacDonald | last post by:
I'm seeing a problem that has me flummoxed. The only thing I can think of is that I'm violating some rule I don't know about. I have some code that does some processing and then does a...
1
by: Sam Wuebben | last post by:
I've been trying to get the values of hidden fields from one of many forms on a page to load into a function. The test page is at: http;//www.mvldesign.com/test/item_pop.html The 'Add to Cart"...
11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
1
by: brianj7675 | last post by:
Basically i'm trying to write a previous next link in order to go to the next page in my database(similar to google's with numbers in the middle for pages the user wants to jump to. My only...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
42
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
6
by: Todd A. Anderson | last post by:
I have a function foo of which I need to get the address. The problem is that when you say "&foo" (or just foo for that matter), you get the address of this function's entry in a jump table and...
2
by: A Wieser | last post by:
Hello, It seems something's going on with regard to the way Visual Studio 2005 implements function pointers. Once upon a time, it was possible to have unmanaged code as follows: ...
1
by: =?Utf-8?B?bGlhbnF0bGl0?= | last post by:
Is using a jump statement more faster than using if statement with a jump statement example for(int i=0; i < 10; i++) { if(a == null) {
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.