473,473 Members | 2,054 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

using fgetc() on array

52 New Member
I am trying to read the first character in an array but my syntax seems to be wrong as it says I have an error. I have read each line a text file into an array and by using fgetc, I'm trying to see if the first character of each line is an "[". My current coding is:

Expand|Select|Wrap|Line Numbers
  1. if(fgetc($invalidcheck[$i])=='[')
Any help would be appreciated. Thanks!
Jun 5 '09 #1
11 3126
Markus
6,050 Recognized Expert Expert
@phpnewbie26
fgetc() expects a file resource handle as it's parameter.

Incidentally, you can check the first character of a string by accessing it like you would an array:

Expand|Select|Wrap|Line Numbers
  1. $string = "[hi]";
  2.  
  3. if ($string[0] == "[") {
  4.     echo "yessir";
  5. }
  6.  
Jun 5 '09 #2
phpnewbie26
52 New Member
hm..doesnt seem to be working. My problem is that I don't know how to point to let's say $string[2] and get it's first character. I tried $string[2][0] but it still didnt work.=/
Jun 5 '09 #3
Markus
6,050 Recognized Expert Expert
@phpnewbie26
Can you provide the full code please.
Jun 5 '09 #4
phpnewbie26
52 New Member
okay.=)

Expand|Select|Wrap|Line Numbers
  1. $invalidcheck=fgets($invalid);
  2. for($i=0;$i<count($invalidcheck);$i++){
  3.     if (i==0){
  4.         $tmp=explode('=',$invalidcheck[$i]);
  5.         if ($tmp[0]!=='MYNAME'){
  6.             die ("typo in name.");
  7.             }
  8.         else{
  9.             continue;
  10.             }
  11.     elseif ($invalidcheck[$i][0]=='['){
  12.         $tmp2=substr($invalidcheck[$i],1,6);
  13.         if ($tmp2==$todaydate){
  14.            $counter=1;
  15.            }
  16.                 else {$counter=0;}
  17.         }
  18.     else{
  19.         $parts2=explode(' % ',$invalid[$i]);
  20.            $parts3=explode('=', $parts2[0]);
  21.         $parts4=explode('.', $parts3[0]);
  22.         $check[0]=$parts4[0];
  23.         $check[1]=$parts4[1];
  24.         if(array_search("$check[0]", $items) && array_search("$check[1]", $items)){
  25.             continue;}
  26.         else{
  27.             or die("Found error in project name/code."); 
  28.             }
  29.         }
  30. fclose($invalid);
  31.  

It's the 11th line. Thanks
Jun 5 '09 #5
dlite922
1,584 Recognized Expert Top Contributor
Troubleshooting is an aquired skill,

right after your for loop do a print_r() or var_dump() on your variable to track what they contain and see if they contain the value you expect them to.

When you say it's not working, we expect there's a PHP error, if there isn't, it's most likely has to do with your input/data.

Cheers,




Dan
Jun 5 '09 #6
phpnewbie26
52 New Member
hm..it still doesnt seem to be working. Let's say I have an array like so:

array([0]=>'red', [1]=>'green', [2]=>'blue')

how do i read let's say..the g in green.
Jun 8 '09 #7
Markus
6,050 Recognized Expert Expert
@phpnewbie26
Expand|Select|Wrap|Line Numbers
  1. $array = array('red', 'green', 'blue'); // Note: this is the same as the example you gave.
  2.  
  3. $element = $array[1]; // green
  4. echo $element[0]; // g
  5.  
Jun 8 '09 #8
Dormilich
8,658 Recognized Expert Moderator Expert
Expand|Select|Wrap|Line Numbers
  1. echo $array[1]{0};
works too
Jun 8 '09 #9
Markus
6,050 Recognized Expert Expert
@Dormilich
Good point :D
Jun 8 '09 #10
Markus
6,050 Recognized Expert Expert
@Dormilich
Just read that in PHP6 the {} notation will be dropped.

http://www.corephp.co.uk/archives/19...for-PHP-6.html

Furthermore, this works too:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $array = array('red', 'blue');
  4.  
  5. echo $array[0][0];
  6. // Outputs 'r'.
  7.  
  8. ?>
Jun 10 '09 #11
phpnewbie26
52 New Member
thanks guys! all of your ideas worked=).
Jun 10 '09 #12

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

Similar topics

13
by: William L. Bahn | last post by:
I'm sure this has been asked before, and I have looked in the FAQ, but I'm looking for an explanation for the following: The functions pairs: gets()/fgets() puts()/fputs() printf()/fprintf()...
10
by: Orion | last post by:
Hey, I was wondering if it was possible to determine if you hit 'EOF' using fseek? I'm using fseek to traverse through the file from start to end and capturing the data into a linked list...
6
by: Kobu | last post by:
Do the "larger" input functions like scanf, gets, fgets use fgetc to take input or an operating system call function like read() (I know it could be any "way", but I'm trying to find out how it's...
2
by: Mark | last post by:
i'm trying to read a file one char at a time into a char array thusly... char buffer; while (readChars(InputFile, buffer, BUFFER_SIZE) != 0) { //not doing anything atm } int readChars(FILE...
3
by: cinsky | last post by:
Hi, While reading ISO C Standard, I found follow text in 7.19.8.1: size_t fread(void *restrict ptr, size_t SIZE, ...) ... For each object, SIZE calls are made to the fgetc function and the...
37
by: 01 | last post by:
#include <stdio.h> int main ( int argc, char *argv ) { if ( argc != 2 ) /* argc should be 2 for correct execution */ { /* We print argv assuming it is the program name */ printf( "usage: %s...
13
by: Ivan | last post by:
Hi, What is the best syntax to use a char to index into an array. /////////////////////////////////// For example int data; data = 1;
24
by: kindrain | last post by:
the code checks whether a.txt has exact the same lines, then write different lines into b.txt Here it is: (before that ,you should creat a.txt) ---------------------- #include<stdio.h>...
4
by: bmerlover | last post by:
How can you read a file in a timer function? I am currently using a do while statement to read a file which works fine. The implementation is in C but I've added the code to a C++ .NET framework GUI...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.