重定向来自txt文件的输入并将其存储在动态创建的数组中并对其进行comap(Redirecting input from txt file and storing it in a dynamically created array and comapring it)
我是C的新手,我今天在这里发帖,以便了解如何修复我已经编写的代码的语法,以便它可以成功地比较argv和我已经创建的数组。 这是我已经处理过的代码,但由于比较语句而无法编译。
#include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { int c = 0; int *array; char *chant; while((c=getchar())!=EOF) { array[0]=c; } chant = strstr(argv[1], array[0]); \\ That is my problem, won't compile here. if(chant==NULL) printf("false\n"); else printf("true\n"); return 0; }
这是我想要的输出在运行时的样子:
./a.out paul <beatles.txt
真正
基本上,argv [1]将包含字符串paul,然后当我在其中引导名为paul的beatles的文本文件时,我的while循环将每个字符存储到数组中。 从那里我希望我的strstr工作,然后导致程序进入else语句并打印true。
对不起,但我对C的有限知识应该归咎于此,我知道argv [1]和array [0]之间的比较不起作用,但我无法找出另一种比较它的聪明方法。 如果我是正确的,argv是一个指针数组的数组......非常奇怪。
编辑;
我用这个修复了while循环,
array = (char *) malloc(15); while((c=getchar())!=EOF) { array[i]=c; i++; }
I am new to C and I am posting here today to get some insight as to how to fix my syntax of my already written code so that it can successfully compare argv and my already created array. Here is my code that I have worked on, but won't compile because of the comparing statement.
#include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { int c = 0; int *array; char *chant; while((c=getchar())!=EOF) { array[0]=c; } chant = strstr(argv[1], array[0]); \\ That is my problem, won't compile here. if(chant==NULL) printf("false\n"); else printf("true\n"); return 0; }
Here's what I want my output to look like when ran:
./a.out paul < beatles.txt
true
Basically, the argv[1] will contain the string paul and then when I direct a text file of the beatles with the name paul in it, my while loop will store each character into the array. From there I want my strstr to work, which then results in the program going to the else statement and printing true.
Sorry, but my limited knowledge of C is to blame here and i know that the comparison between argv[1] and array[0] won't work, but I can't figure out another clever way of comparing it. If i am correct, argv is an array of an array of pointers...Very weird .
EDIT;
I fixed the while loop with this,
array = (char *) malloc(15); while((c=getchar())!=EOF) { array[i]=c; i++; }
原文:https://stackoverflow.com/questions/26322047
最满意答案
阅读本文 - 自定义依赖项属性
就像是 -
public static readonly DependencyProperty MyPersonValueProperty = DependencyProperty.Register( "MyPersonValue", typeof(MyPerson), typeof(MyPersonControl), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnPersonChanged) ) ); public MyPerson ThePerson { get { return (MyPerson)GetValue(MyPersonValueProperty); } set { SetValue(MyPersonValueProperty, value); } } private static void OnPersonChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { // Property change code here }
Read on this article - Custom Dependency Properties
Something like -
public static readonly DependencyProperty MyPersonValueProperty = DependencyProperty.Register( "MyPersonValue", typeof(MyPerson), typeof(MyPersonControl), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnPersonChanged) ) ); public MyPerson ThePerson { get { return (MyPerson)GetValue(MyPersonValueProperty); } set { SetValue(MyPersonValueProperty, value); } } private static void OnPersonChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { // Property change code here }
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
DependencyProperty ValidateValueCallback问题(DependencyProperty ValidateValueCallback question)[2022-07-23]
对照一组静态约束,验证回调用作针对给定输入值的健全性检查。 在验证回调中,检查正值是验证的正确用法,但是检查其他属性不是。 如果您需要确保给定值小于从属属性,则应该使用属性强制 ,如下所示: public static DependencyProperty AProperty = DependencyProperty.Register("A", typeof(double), typeof(ValidateTest), new PropertyMetadata(1.0, null, coerceValue) ... -
阅读本文 - 自定义依赖项属性 就像是 - public static readonly DependencyProperty MyPersonValueProperty = DependencyProperty.Register( "MyPersonValue", typeof(MyPerson), typeof(MyPersonControl), new FrameworkPropertyMetadata(null, FrameworkPro ...
-
具有自定义属性的XAML DependencyProperty(类)(XAML DependencyProperty with custom properties (class))[2022-03-04]
您是否考虑过使用自定义转换器? 它将转换依赖项属性的值,但是您定义。 您的案例中的一个例子可能是: public class StatusToTextConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { try { bool? b = (bool ... -
我担心,绑定不适用于任何类型的DataGridColumns。 它们没有DataContext,并且它们不是可视树的一部分。 要使列中的绑定起作用,您必须使用代理项来设置DataContext或其他类似的解决方法。 在这种情况下, 我解释了代理项方法 ,对于VisualBrush类,但它对DataGridColumn应该是相同的。 我假设IsUseDecimal是ViewModel中的属性 (或DataGrid具有的任何DataContext)。 代理要素: public class DataCon ...
-
好的,如果有人遇到同样的问题,这里是解决方案:只需将DependencyProperty的可见性从私有更改为公共。 实际上非常明显...: - / Ok, in case anyone runs into the same problem, here is the solution: Just change the visibility of the DependencyProperty from private to public. Quite obvious actually... :-/
-
DependencyProperty无法处理自定义UserControl(DependencyProperty not working on custom UserControl)[2021-12-21]
您正在错误地注册Icon属性。 在其注册方法中,您需要指定DP名称,即代替“ IconProperty ”,它应该是“ Icon ” - public static readonly DependencyProperty IconProperty = DependencyProperty.Register( "Icon", typeof(ImageSource), typeof(MyCustomControl), new Pro ... -
公开ViewModel事件以绑定到自定义DependencyProperty(Expose a ViewModel event for binding to a custom DependencyProperty)[2022-05-30]
查看关闭意味着关闭窗口事件。 所以你基本上想要对视图中的事件做出反应。 我最近读了这篇论文,有一个非常好的形象 并且还提到了EventBehavior存在。 如果你不想要任何代码,最好的办法就是使用行为。 行为是一个简单的附加属性,可以执行操作,例如上升的应用程序范围的命令, ViewModel可以在没有MVVM问题的情况下捕获这些命令。 以下是行为示例: public static class FreezeBehavior { public static bool GetIsFrozen(Depe ... -
您需要在动画上将EnableDependentAnimation设置为true。 原因是阻止动画在UI线程上更改的属性(依赖属性),从而产生不连贯的动画。 建议仅为RenderTransform属性或Opacity等独立属性设置动画,而不是为Width , Margin , Canvas.X或自定义属性等依赖属性设置动画。 You need to set EnableDependentAnimation to true on the animation. The reason is to discourag ...
-
可能有更好的方法来做到这一点,但这是一种方法。 首先,使用SlideInFrom方向创建一个枚举。 确保此枚举不是在SlideGrid类中定义的,否则将无效(至少它不适合我)。 public enum SlideInFromDirection { Left = 0, Top = 1, Right = 2, Bottom = 3, } 接下来,在ControlTemplate中为Y属性创建第二个DoubleAnimation,并将X和Y DoubleAnimations命名为 ...