The Legion  
 

Go Back   The Legion > Public Legion Forums > Legion Lounge > Entertainment & Technology

Entertainment & Technology Discussion of music, games, movies, and computer technology.

Reply
 
Thread Tools Display Modes
Old 10-22-2011, 09:16 PM   #1
SpecialMike
oaty the dimejizz
 
SpecialMike's Avatar
 
Nation: Alemania
Current Wars | Foreign Aid

Join Date: Nov 2010
Location: Florida
Posts: 3,031

Default C question

Much like roddney, I also need help with my programming. I'm working on this C parser (correct word?) that will work on a copy and pasted nation screen. The problem I'm running into here is the fact that printing a string from an array is not working at all for some reason, but the array definitely contains the characters, as proven by the printf() in the loop that reads the wanted data. All that is printed to file is two spaces, at least I think they are spaces. There is probably some ridiculously easier way to do this, and you are free to point that out as well. Without further adieu, here is the code:

PHP Code:
char readInfo(FILE *readchar beginningchar endchar string[]){
    
char icharacter;
    
int j;
    while(
1){
       
= (char)fgetc(read); //reads the first character
       
if(== (char)beginning){ //if it is the character before the first wanted character, begin parsing into string[]
           
while(1){
               
character = (char)fgetc(read);
               
0;
               if (
character != (char)end){ //parse the information until the character is the end character
                   
string[j] = character;
                   
printf("%c %c\n"string[j], character); //just a debugging line so I can see that the array does, in fact, get the characters
               
}
               else
                   break;
               
j++;
           }
        
printf("%s"string); //this is to see if the array will print as a string, which it isn't for some reason...
           
break;
       }
    }
}

void parseFile(){
    
FILE *read;
    
FILE *write;
    
char fName[20] = { NULL };
    
char sName[20] = { NULL };
    
char rulerName[20] = { NULL };
    
char nationName[20] = { NULL };
    
char govType[20] = { NULL };

    while(
1){
        
printf("Enter a File Name (maximum of 20 characters):\n");
        
scanf("%s"fName);
        if((
read fopen(fName"r")) == NULL)
            
printf("Cannot open file, please try again.\n");
        else
            break;
    }
    while(
1){
        
printf("Enter a File Name to save as (maximum of 20 characters):\n");
        
scanf("%s"sName);
        if((
write fopen(sName"w")) == NULL)
            
printf("Cannot open file, please try again.\n");
        else
            break;
    }

    
readInfo(read':''['nationName); //read in nationName
    
printf("%s"nationName);  //like above, printing as a string isn't helping
    
fprintf(write"%s"nationName);// print nationName to file
    
fclose(read);
    
fclose(write);

Also, to be sure, I'm passing the array correctly, right?

I guess I could use a loop to print every character individually, but that just seems like more work than necessary.
__________________




Spiced Rum War Vet


Doomhouse WarTetris WarODN WarNuclear SurvivorIch Dien Gold AwardGeneral Government Service RibbonInspector General's Service RibbonSilver Admiralty RibbonAdmiralty Wartime Service RibbonAdmiralty Exemplary Service RibbonEconomics Distinguished Service AwardMost Improved Legionnaire
SpecialMike is offline   Reply With Quote
Old 10-22-2011, 09:31 PM   #2
MrOtingocni
Optio
 
MrOtingocni's Avatar
 
Nation: Kzoppistan
Current Wars | Foreign Aid

Join Date: Sep 2011
Posts: 730

Default Re: C question

Quote:
Originally Posted by SpecialMike View Post

I guess I could use a loop to print every character individually, but that just seems like more work than necessary.
Well, much like in Roddeny's post, I don't know anything about C, which makes me wholly unqualified to give advice, but since that's never stopped me before, in Qbasic (stop laughing), you do have to loop through the array assigning elements to the variable you want to print from. (iirc, printing an array without a specified element only gives you an empty string) You can read data from a file and assign it to an element in the array the same way.

This was back when punch cards were made out of stone tablets, so... I'm sure someone else knows.
Tetris WarNuclear Survivor
MrOtingocni is offline   Reply With Quote
Old 10-22-2011, 09:38 PM   #3
SpecialMike
oaty the dimejizz
 
SpecialMike's Avatar
 
Nation: Alemania
Current Wars | Foreign Aid

Join Date: Nov 2010
Location: Florida
Posts: 3,031

Default Re: C question

Quote:
Originally Posted by MrOtingocni View Post
Well, much like in Roddeny's post, I don't know anything about C, which makes me wholly unqualified to give advice, but since that's never stopped me before, in Qbasic (stop laughing), you do have to loop through the array assigning elements to the variable you want to print from. (iirc, printing an array without a specified element only gives you an empty string) You can read data from a file and assign it to an element in the array the same way.

This was back when punch cards were made out of stone tablets, so... I'm sure someone else knows.
Not laughing, I had to learn Qbasic as well.

Anyway, in C, you can print an entire string with the %s as long as the array has the string characters starting on index 0.... oh wow I just figured out what is wrong with this as I was typing. I have a space before the : and S and the end of a string is a whitespace character iirc. Lets see if this works.

EDIT: Oh boy, progress, I got it to print an 'e' which is the last character (it should be SpecialMike). I added an if(character != ' ') before assigning the character to the array.

Also, as I was saying, in C you don't HAVE to loop and print each character individually, but you can. I chose the character-by-character method of assignment because of the broadness of different things in the copy and pasted file.
__________________




Spiced Rum War Vet


Doomhouse WarTetris WarODN WarNuclear SurvivorIch Dien Gold AwardGeneral Government Service RibbonInspector General's Service RibbonSilver Admiralty RibbonAdmiralty Wartime Service RibbonAdmiralty Exemplary Service RibbonEconomics Distinguished Service AwardMost Improved Legionnaire

Last edited by SpecialMike; 10-22-2011 at 09:46 PM.
SpecialMike is offline   Reply With Quote
Old 10-22-2011, 09:43 PM   #4
MrOtingocni
Optio
 
MrOtingocni's Avatar
 
Nation: Kzoppistan
Current Wars | Foreign Aid

Join Date: Sep 2011
Posts: 730

Default Re: C question

Giving vague advice based on outdated methods with a conclusion to seek more advice until the other person finally just figures it out on their own. Hmmm...

I should be a professional consultant. That's pretty much how they work.

Let is know if your solution fixes it.
Tetris WarNuclear Survivor
MrOtingocni is offline   Reply With Quote
Old 10-22-2011, 09:55 PM   #5
SpecialMike
oaty the dimejizz
 
SpecialMike's Avatar
 
Nation: Alemania
Current Wars | Foreign Aid

Join Date: Nov 2010
Location: Florida
Posts: 3,031

Default Re: C question

It sorta worked, but I think there is another weird character in there like a tab or something that isn't just a space that I really don't feel like figuring out right now because I'm tired and want some sleep. That, and I'm lazy. I'll figure it out in the morning, I'm tired of smashing my head against the keyboard for hours on end.

Also, I shall keep that in mind, when someone asks for help just give a vague answer and tell them to wait for someone who knows what they are working on to help. You're on a streak with that one.

Mornings are great for thinking. I'm an idiot and was setting j to 0 every iteration, so the first index of the array was constantly being overwritten. Now it works, and I feel accomplished, but there is probably an easier way that I don't feel like figuring out right now.
__________________




Spiced Rum War Vet


Doomhouse WarTetris WarODN WarNuclear SurvivorIch Dien Gold AwardGeneral Government Service RibbonInspector General's Service RibbonSilver Admiralty RibbonAdmiralty Wartime Service RibbonAdmiralty Exemplary Service RibbonEconomics Distinguished Service AwardMost Improved Legionnaire

Last edited by SpecialMike; 10-23-2011 at 06:57 AM.
SpecialMike is offline   Reply With Quote
Old 12-21-2011, 09:24 PM   #6
Presldent Marin
 
Nation: Presldent Marin
Current Wars | Foreign Aid

Discord name: PresidentMarin

Alliance:
The Last Republic

Join Date: Dec 2011
Posts: 192

Default Re: C question

spam spam spam spam spam spam spam spam spam spam spam spam spam spam spam spam spam spams spam spam spam
Presldent Marin is offline   Reply With Quote
Old 12-21-2011, 09:25 PM   #7
Legionnaire
 
Legionnaire's Avatar
 
Nation: Legionia
Current Wars | Foreign Aid

Join Date: Feb 2008
Posts: 13,653

Default Re: C question

__________________
The Dove War / Valhalla WarUnjust WarCoalition WarKarma WarTetris WarPurple HeartNuclear SurvivorLoyaltyIch Dien Lifetime AwardConsulate's Legionnaire of the Term RibbonDistinguished Donator AwardEconomics General Service AwardForeign Ministry Nation Exchange RibbonSpammiest LegionnaireFunniest Legionnaire
Legionnaire is offline   Reply With Quote
Reply

  The Legion > Public Legion Forums > Legion Lounge > Entertainment & Technology

Tags
question


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

 
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
© The Legion | Terms of Use | Privacy Policy

All times are GMT -8. The time now is 04:03 PM.
no new posts