473,379 Members | 1,185 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,379 software developers and data experts.

appending char to string in c

56
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main() {
  6.     char states [6][10]={"start","InNum","InID","Done","InASG","InCOM"};
  7.     char state[] = "start";
  8.     char Final[10] ;
  9.     char temp[2] ;
  10.     int n = 1;
  11.     char ch,file_name[25];
  12.     FILE *fp;
  13.     fp = fopen("D:\\chech.txt","r");
  14.  
  15.     if( fp == NULL ) { 
  16.         perror("Error while opening the file.\n");
  17.         exit(EXIT_FAILURE);
  18.     } 
  19.  
  20.     printf(state);
  21.     while( ( ch = fgetc(fp) ) != EOF ){
  22.  
  23. //------------------------------------------------------------------------------------
  24. //Start state
  25.  
  26.         if((ch=='0' || ch=='1' || ch=='2' || ch=='3' || ch=='4' || ch=='5' || ch=='6' || ch=='7' || ch=='8' || ch=='9')&&(state=="start"))
  27.  
  28.         {
  29.             strcpy(state,states[1]);
  30.  
  31.             temp[ 0 ] = ch;
  32.             temp[ 1 ] = '\0';
  33.  
  34.             strcat(Final,temp);
  35.  
  36.             printf(Final);
  37.         }    
  38.         }
  39.  
  40.     fclose(fp);
  41.     printf("\r\n");
  42.     return 0;
  43. }
i am trying to take each character from a file and put it in a string and print this string
but when i write this code , it doesn't give any output
Dec 1 '13 #1

✓ answered by weaknessforcats

Expand|Select|Wrap|Line Numbers
  1. state=="start"
You can't compare strings this way. If you need to know whether two strings are equal then you use the strcmp library function:

Expand|Select|Wrap|Line Numbers
  1. strcmp(state, "start");
If this call returns a zero, then the strings are equal.

The == operator will check if two things are equal but in the case of arrays, it will just compare the addresses of the arrays rather than comparing the contents of the arrays. Comparing addresses will always be false since each array has its own address in memory.

6 1575
weaknessforcats
9,208 Expert Mod 8TB
Part of your problem is here:

Expand|Select|Wrap|Line Numbers
  1. etc...|| ch=='8' || ch=='9')&&(state=="start"))
state is an array and "start" is another array.

By definition the name of an array is the address of element 0. Therefore, state is the address of state[0] and this address will always be different from the address of element 0 in "start".

Therefore, your "if" statement will always be false.
Dec 1 '13 #2
Norgy
56
still can't understand the problem
could you explain more please?
Dec 1 '13 #3
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. state=="start"
You can't compare strings this way. If you need to know whether two strings are equal then you use the strcmp library function:

Expand|Select|Wrap|Line Numbers
  1. strcmp(state, "start");
If this call returns a zero, then the strings are equal.

The == operator will check if two things are equal but in the case of arrays, it will just compare the addresses of the arrays rather than comparing the contents of the arrays. Comparing addresses will always be false since each array has its own address in memory.
Dec 1 '13 #4
Norgy
56
thanks a lot =)
i got it
Dec 1 '13 #5
donbock
2,426 Expert 2GB
By the way, you can replace
Expand|Select|Wrap|Line Numbers
  1. (ch=='0' || ch=='1' || ch=='2' || ch=='3' || ch=='4' || ch=='5' || ch=='6' || ch=='7' || ch=='8' || ch=='9')
with
Expand|Select|Wrap|Line Numbers
  1. isdigit(ch)
Look up the isdigit function to find out which standard header file you need to #include.
Dec 2 '13 #6
Norgy
56
yes, i have improved the code and put isdigit(ch) instead of 1,2,3,4,5,6,7,8,9
and included ctype.h in the header
thank you :)
Dec 5 '13 #7

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

Similar topics

3
by: Andreas Müller | last post by:
hi @all, I have a char* string array and i want to fill it up in a while loop: while(i<3){ length = read(STDIN_FILENO, inputBuffer, sizeof(inputBuffer); ... } after this the array should...
4
by: Pokerkook | last post by:
Hello, If anybody could help me with this I would greatly appreciate it. Or at least tell me why I get the output of this garbage: 49 49 10 49 52
18
by: Paul Roberts | last post by:
Hiya I have a string with about 300+ characters, how can i insert a line break or another character every 50 characters for the whole string? Paul Roberts
33
by: Jordan Tiona | last post by:
How can I make one of these? I'm trying to get my program to store a string into a variable, but it only stores one line. -- "No eye has seen, no ear has heard, no mind can conceive what God...
14
by: gustavo | last post by:
I was looking at the Sendmail's source code, and i've got confused about this kind of initialization: ------------------------ struct prival PrivacyValues = { { "public", PRIV_PUBLIC }, {...
33
by: Michael B Allen | last post by:
Hello, Early on I decided that all text (what most people call "strings" ) in my code would be unsigned char *. The reasoning is that the elements of these arrays are decidedly not signed. In...
13
by: Hongyu | last post by:
Hi, I have a datetime char string returned from ctime_r, and it is in the format like ""Wed Jun 30 21:49:08 1993\n\0", which has 26 chars including the last terminate char '\0', and i would...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.